Visit Our UNIX and Linux User Community


Arithmetic but keep 2 decimal places


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Arithmetic but keep 2 decimal places
# 1  
Old 02-11-2013
Arithmetic but keep 2 decimal places

I am trying to perform arithmetric, for example, to increment the value of variable $a (say 3) by 0.05 but when I tried the following expression

Code:
let a=a+0.05

or
Code:
a=$((a+0.05))

both returned

Code:
3.0499999999999998

I want to keep 2 decimal places so it returns 3.05 instead.
# 2  
Old 02-11-2013
Code:
a=$(echo "scale=2; 3+0.05" | bc)

# 3  
Old 02-11-2013
Code:
echo 3 | awk '{print $0+0.05}'
3.05

Code:
a=$(echo $a | awk '{print $0+0.05}')

# 4  
Old 02-11-2013
Hi.
Code:
printf "%s %.2f\n" $a $a
3.0499999999999998 3.05

Best wishes ... cheers, drl
# 5  
Old 02-11-2013
Quote:
Originally Posted by drl
Hi.
Code:
printf "%s %.2f\n" $a $a
3.0499999999999998 3.05

Best wishes ... cheers, drl
This just clean up the mess not the source of the problem.
Using awk or bc and you get correct result.
# 6  
Old 02-11-2013
Quote:
Originally Posted by Jotne
This just clean up the mess not the source of the problem.
Using awk or bc and you get correct result.
It works on your system in your case but this is not guaranteed. It uses floating point numbers too, which have the same issues anywhere.

bc, being an arbitrary precision calculator, can indeed give you the exact result, though fiddling is necessary to prevent it stripping off the decimal points.
# 7  
Old 02-12-2013
Thanks everyone!

Previous Thread | Next Thread
Test Your Knowledge in Computers #581
Difficulty: Easy
In a typical unix-based system, everything is a file.
True or False?

9 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

Sum the fields with 6 decimal places - getting only 2 decimal places as output

I used the below script to Sum up a field in a file based on some unique values. But the problem is when it is summing up the units, it is truncating to 2 decimals and not 6 decimals as in the input file (Input file has the units with up to 6 Decimals – Sample data below, when the units in the 2... (4 Replies)
Discussion started by: brlsubbu
4 Replies

3. Shell Programming and Scripting

Printing with decimal places from last 4 digits

I have input file like below, 201424|9999|OSS|622010|RGT|00378228764 201424|8888|OM|587079|RGT|00284329675 201424|7777|OM|587076|RGT|00128671024 201424|6666|OM|581528|RGT|00113552084 Output should be like below, should add decimal (.) from last 4 digits. ... (2 Replies)
Discussion started by: vinothsekark
2 Replies

4. Shell Programming and Scripting

How to round up value upto 2 decimal places using sed?

Please help me in rounding up value upto 2 decimal palces using sed command #!/usr/bin/bash a=15.42 b=13.33 c=`echo $a*$b |bc -l` echo $c above code is is giving output "205.5486" but i want the output as "205.55" Thank you... (15 Replies)
Discussion started by: ranabhavish
15 Replies

5. Shell Programming and Scripting

Bash Rounding to 2 decimal places

I have a number in a bash variable n, and want to round it to 2 decimal places. How can I do that? n=0.0867268 Need to have num=0.09 (1 Reply)
Discussion started by: kristinu
1 Replies

6. Shell Programming and Scripting

how to do decimal arithmetic in shell script

hi, I have a file with decimal/non-decimal values $ cat b22 373 164 92 62 20 131 94 12 129 111 95 154 37 15 447 25 7.4 135 77 122 32 92 70 57 37 42 72 17 13 97 40 41 53 22 80 71 29 87 23 31 273 6.2 12K 43 44 45 22 11 7.7 13 18 173 36 20 18 13 56 67 104 53 5.4 241 19 13 3.8 38 14 31 329 16 155... (8 Replies)
Discussion started by: sam05121988
8 Replies

7. Shell Programming and Scripting

Decimal places

i need to multiplay a number with 1.00.. so that the output should contain two decimal places at end.. for example... 236 * 1.00 = 236.00 245.8 * 1.00 = 245.80 but when i perform multiplication it shows output as. 236 245.8 can anyone help me to get the actual output of... (11 Replies)
Discussion started by: arunmanas
11 Replies

8. Shell Programming and Scripting

Four decimal places with awk

i have a script in which awk prints "($2-1700)/10000" and the answer is -0.07,but i want the answer in 4 decimal places. that is -0.0700. How can i sue awk to get my results in four decimal places (4 Replies)
Discussion started by: tomjones
4 Replies

9. Shell Programming and Scripting

trimm up the decimal places in output

I have a perl script that reports the avg time of a application call and the total number of calls. This works fine, however I would like to trim the number of decimal places reported from 12 to like 3 and I don't know how. Any suggestions? Here is what I use to get the avg time... for $eRef (... (2 Replies)
Discussion started by: theninja
2 Replies

Featured Tech Videos