|
From: BJ on 28 Aug 2006 04:53 I'm trying to get two (2) decimal places in the code below with an accurate number of "43.21", but I only get "43.00". If I change "scale=2" to "scale=4", I get "43.2100". What do I need to do to get an output of "43.21"? Thanks. #!/bin/bash TOP=4321.0 BOTTOM=10000.0 RESULT=$(echo "scale=2;$TOP/$BOTTOM*100"|bc) echo $RESULT
From: Eric Moors on 28 Aug 2006 06:20 BJ wrote: > echo "scale=2;$TOP/$BOTTOM*100"|bc your $TOP/$BOTTOM requires 4 digits accuracy. so try: echo "scale=2;100*$TOP/$BOTTOM"|bc Eric
From: BJ on 28 Aug 2006 07:25 > > echo "scale=2;$TOP/$BOTTOM*100"|bc > > your $TOP/$BOTTOM requires 4 digits accuracy. > > so try: > echo "scale=2;100*$TOP/$BOTTOM"|bc > > Eric Thanks! That did it, but why did moving the "100" in front of the division make a difference?
From: Eric Moors on 28 Aug 2006 08:05 BJ wrote: >> > echo "scale=2;$TOP/$BOTTOM*100"|bc >> >> your $TOP/$BOTTOM requires 4 digits accuracy. >> >> so try: >> echo "scale=2;100*$TOP/$BOTTOM"|bc >> >> Eric > > Thanks! That did it, but why did moving the "100" in front of the > division make a difference? Like I mentioned $TOP/$BOTTOM (4321/10000) requires 4 digits accuracy as it results to 0.4321. If you choose scale=2, it is trimmed down to 0.43. Then the multiplication with 100 takes place, resulting into 43.00 Moving 100 to the front results in (100*4321)/10000 = 432100/10000 = 43.21 So not using more then 2 digits of accuracy here doesn't loose info. Eric
From: Jon LaBadie on 28 Aug 2006 11:50 Eric Moors wrote: > BJ wrote: > >>>> echo "scale=2;$TOP/$BOTTOM*100"|bc >>> your $TOP/$BOTTOM requires 4 digits accuracy. >>> >>> so try: >>> echo "scale=2;100*$TOP/$BOTTOM"|bc >>> >>> Eric >> Thanks! That did it, but why did moving the "100" in front of the >> division make a difference? > > Like I mentioned $TOP/$BOTTOM (4321/10000) requires 4 digits accuracy > as it results to 0.4321. If you choose scale=2, it is trimmed down to > 0.43. Then the multiplication with 100 takes place, resulting into 43.00 > Moving 100 to the front results in (100*4321)/10000 = 432100/10000 = 43.21 > So not using more then 2 digits of accuracy here doesn't loose info. > Any way to have internal calculations done to higher precision but presented with lower? Aside from something like: scale = 20; <calc> ; scale = 2 ; . * 1
|
Next
|
Last
Pages: 1 2 Prev: Newbie question: delete newline character with sed Next: How to sum csv column data in ksh |