truncate string to integer or decimal


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting truncate string to integer or decimal
# 1  
Old 10-05-2011
truncate string to integer or decimal

From strings stored in variables, I need to isolate and use the first numerical value contained within them. I will need to know how to produce an integer as well as a floating point decimal. It needs to work on any string regardless of what types of characters (if any) are preceding or following the first numerical value contained in the string.

stringA="123.456:789 example string"
stringB="example string 123.456:789 example string"

Both strings should produce "123" when calling for an integer.
Both strings should produce "123.456" when calling for a floating point decimal.

I also would like to know how to replicate JavaScript's parseInt(), and parseFloat() functions. So stringA would produce a similar result, but stringB would result with NaN, because it doesn't begin with a numerical value. It would essentially be truncating the string to the end of the leading series of numerical characters, unless the leading character is non-numerical.

I would like to see several simple/efficient ways to accomplish these tasks. If anyone has ideas, please share.

Sorry, I'm just a JS developer, but I can't believe I haven't been able to find a good example. I've caked my question with keywords I was using, for anyone else feeling my pain. Smilie

---------- Post updated at 05:51 PM ---------- Previous update was at 05:38 PM ----------

One of my co-developers got back to me on part of my question. Here's a start. This doesn't strip off any leading non-numerical characters, but does emulate JavaScript parseInt and parseFloat.

Code:
stringA="123.456:789 example string"

newAFloat=`echo $stringA | egrep -o  '^[0-9]+(\.[0-9]+)?'`
echo $newAFloat
#123.456
newAInt=`echo $stringA | egrep -o  '^[0-9]+'`
echo $newAInt
#123

# 2  
Old 10-05-2011
A few more with Perl:

Code:
$
$ stringA="123.456:789 example string"
$ stringB="example string 123.456:789 example string"
$ stringC="example string .123.456:789 example string"
$
$ # decimals
$ echo $stringA | perl -plne 's/^.*?(\d+).*?$/$1/'
123
$ echo $stringB | perl -plne 's/^.*?(\d+).*?$/$1/'
123
$ echo $stringC | perl -plne 's/^.*?(\d+).*?$/$1/'
123
$
$ # floats
$ echo $stringA | perl -plne 's/^.*?(\d*\.\d+|\d+\.\d+).*?$/$1/'
123.456
$ echo $stringB | perl -plne 's/^.*?(\d*\.\d+|\d+\.\d+).*?$/$1/'
123.456
$ echo $stringC | perl -plne 's/^.*?(\d*\.\d+|\d+\.\d+).*?$/$1/'
.123
$
$

tyler_durden
# 3  
Old 10-05-2011
Awesome, awesome, awesome.
# 4  
Old 10-06-2011
Code:
$ echo $str | sed 's,[aA-zZ],,g' | cut -d'.' -f1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate the constant e to 14+ decimal places using integer maths.

Hi guys... I am loving this integer maths thing. 64 bit systems are certainly easier than 32 bit, but hey, I don't intend to leave out my fav' platform. Using one of the 'Brothers' methods, URL inside the code. #!/bin/sh # # #!/usr/local/bin/dash # e_constant.sh # Brother's formula . #... (2 Replies)
Discussion started by: wisecracker
2 Replies

2. Shell Programming and Scripting

Column value can be Two integer post decimal or blank

Hi Experts, Need your advice. I have a csv file in which column value can contain two integer post decimal(like 1.00, 13.00,12.15, 2.43) or blank. Tried the below code but not working. awk -F "|" '{ if ($39 !~ /^+\.{2}$ || $39 != "") {print "165: Quantity decimal values are not correct... (2 Replies)
Discussion started by: as7951
2 Replies

3. Shell Programming and Scripting

[Solved] need to convert decimal to integer

Using below command awk 'NR==FNR{A=$1;next} {sum+=($2*A)}END{OFMT="%20f";print int(sum)}' Market.txt Product.txt answer:351770174.00000 how to convert this to 351770174. when i try with below command i am getting different result. awk 'NR==FNR{A=$1;next}... (3 Replies)
Discussion started by: katakamvivek
3 Replies

4. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

5. UNIX for Dummies Questions & Answers

compare decimal and integer values in if in bash shell

i need to do camparisions like the below. For the case when first=10 and second=9.9 the scripts displays process failed. I need to be able to convert the values to integer before doing the comparision. Like 9.9 should be rounded over to 10 before doing comparision. Please advice how can... (3 Replies)
Discussion started by: nehagupta
3 Replies

6. Shell Programming and Scripting

Truncate string variable

Hi, I want to truncate a string variable, returned in the script. In perl I used the below and it worked. BRNo=BR12345 $BR = substr($BRNo, 2, 7) How can I do it in sh. Thanks ! (8 Replies)
Discussion started by: script2010
8 Replies

7. Shell Programming and Scripting

Converting decimal to integer

The shell mentioned below will show a warning if the page takes more than 6 seconds to load. The problem is that myduration variable is not an integer. How do I convert it to integer? myduration=$(curl http://192.168.50.1/mantisbt/view.php?id=1 -w %{time_total}) > /dev/null ; ] && echo... (3 Replies)
Discussion started by: shantanuo
3 Replies

8. Shell Programming and Scripting

Converting a decimal into integer

Hi all, I'm trying to convert a decimal number into an integer number; I'm doing this: n=`echo |awk '{ print "'"$mem"'"*10}'` where the variable mem is equal to 3.7 I'd like to obtain 37, but the expression above gives me 30. Help please!!!! thx a lot (4 Replies)
Discussion started by: idro
4 Replies

9. Shell Programming and Scripting

Compare integer value with decimal

Hi all, I have a issue... Is it possible to compare integer value with decimal value. If it is not possible,then how can i compare 2 decimal values in born shell.thanks! (3 Replies)
Discussion started by: MARY76
3 Replies

10. Shell Programming and Scripting

Help: How do I ADD non-integer (decimal) values?

I am trying to create a script that will read from a file two non-integer values (decimals) and add those values together. For example, I want to add 1.51 and -2.37 together and get the sum. Any ideas? Thanks! (2 Replies)
Discussion started by: limshady411
2 Replies
Login or Register to Ask a Question