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...
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...
One of the ways:
c=`echo "scale=2;$a*$b" |bc -l`
Thanks but it is giving output "205.54" instead of "205.55"
Using CygWin bash:-
AMIGA:~> text=42.377821
AMIGA:~> printf "%0.2f\n" $text
42.38
AMIGA:~> text=42.843821
AMIGA:~> printf "%0.2f\n" $text
42.84
AMIGA:~> _
[LEFT]is there any sed command what can i use?
[/LEFT]
A quick and dirty way:
echo "scale=2;($a*$b)+.01"|bc
sed cannot do math. sed cannot round numbers.
This seemingly simple question is difficult to solve completely in bc itself because it simply does not calculate in that fashion. The rounding at least can be done by adding 0.005 and truncating down to two decimal places. I don't think adding 0.01 is correct.
a=15.42
b=13.33
c=`echo "($a*$b)+0.005" |bc -l`
OLDIFS="$IFS" ; IFS="."
set -- $c
c="${1}.${2:0:2}"
IFS="$OLDIFS"
echo $c
Dear in2nix4life
but it is not working in below case, output should be "200.92" but it is giving "200.93"
#!/usr/bin/bash
a=15.42
b=13.03
c=`echo "scale=2;($a*$b)+.01" |bc -l`
echo $c
Again, I think 0.005 is the correct value to add, not 0.01. 0.01 will round EVERY number up, 0.005 will only round up by halves. But then bc insists on outputting three or more digits and the number must be truncated after, try my code.
If you really really really insist on a sed solution, try this as a starting point; you will need to extend it for further carry operations:
echo 205.5686 | sed 's/\(\...\)[0-4].*/\1/;t;
s/\(\..\)0.*/\11/;t
s/\(\..\)1.*/\12/;t
s/\(\..\)2.*/\13/;t
s/\(\..\)3.*/\14/;t
s/\(\..\)4.*/\15/;t
s/\(\..\)5.*/\16/;t
s/\(\..\)6.*/\17/;t
s/\(\..\)7.*/\18/;t
s/\(\..\)8.*/\19/;t
s/\(\..\)9.*/\10/;t
'
205.57
Longhand using OSX 10.7.5, default bash terminal.
#!/bin/bash --posix
# Save IFS.
ifs_str="$IFS"
IFS="."
# Number with 3rd decinal place as 6. NOTE: no zero at the start but "-" instead!
summed_number=-.45677
# Create an array.
round=($summed_number)
# Ensure a roundup number!
roundup=9
# Print it to the terminal.
printf "%0.2f\n" ${round[0]}.${round[1]:0:2}$roundup
# Repeat above with 3rd decimal place as 3.
summed_number=1097.31367
round=($summed_number)
printf "%0.2f\n" ${round[0]}.${round[1]:0:2}$roundup
# Reset IFS.
IFS="$ifs_str"
# You now have the building blocks using bash builtins.
# I will leave you to create the required loops.
Results...
Last login: Fri Nov 15 18:44:29 on ttys000
AMIGA:barrywalker~> chmod 755 roundup.sh
AMIGA:barrywalker~> ./roundup.sh
-0.46
1097.32
AMIGA:barrywalker~> _
EDIT:
IMPORTANT! This does NOT account for +ve or -ve _integer_ numbers, zero and
floating point _equivalents_ of _integer_ numbers; so be well aware!
If you have a large number of such numbers to process, you might be better off using the Korn shell, which has builtin floating point support, because of the overhead in invoking the bc utility.
For example:
#!/bin/ksh93
a=15.42
b=13.33
c=$(printf "%.2f\n" $(( a * b )))
echo $c
outputs 205.55
Since I do not like to use bc
because its not in all image. And normal I use bash
. To do use math I use awk
. This should work in most image.
Sed
is not tool for math, so I do not see any reason for using that.
a=15.42
b=13.33
c=$(awk -v a=$a -v b=$b 'BEGIN {printf "%.2f\n",a*b}')
echo c$
205.55
Use bc
for calculation, let printf
do the rounding!
a=15.42
b=13.33
c=`echo "$a*$b" | bc -l`
printf "%.2f\n" $c
NB you must quote the expression, otherwise the shell tries to match a *
against the current directory.
Better you try awk it's very simple and easy
$ awk -v a=15.42 -v b=13.33 'BEGIN{printf "%5.2f\n", a*b}'
205.55
for variable
$ my_variable=$(awk -v a=15.42 -v b=13.33 'BEGIN{printf "%5.2f\n", a*b}')
$ echo $my_variable
205.55
OR just print
$ awk -v a=15.42 -v b=13.33 'BEGIN{OFMT="%5.2f";print a*b}'
205.55
@MadeInGermany
BC is not installed as default on many system
@Akshay Hegde
Same as I posted in post #13