![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Rounding off the value of Floating point value | damansingh | Shell Programming and Scripting | 7 | 05-21-2008 09:46 AM |
| Rounding problem | shash | UNIX for Dummies Questions & Answers | 2 | 01-18-2007 05:33 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Rounding off using BC.
Hello again.
I'm trying to use BC to calculate some numbers in a shell script. I want to have the numbers rounded off to 1 decimal place. for example: initsize=1566720 zipsize=4733 I'm trying to get the ratio between them. the equation is: (($initsize-$zipsize)/$initsize)*100 so I'm trying to do this: echo "scale=1; (($initsize-$zipsize)/$initsize)*100" | bc but the answer is coming out as 90.0 instead of 99.6 it's obviously because the first part in the brackets is being rounded off before getting multiplied by 100. I tried plsitting it up to work out hte first bit at 3 decimal places, then multiplying it by 100 with 1 deciaml place. but it still showed up with 3 decimal places. any ideas? |
|
||||
|
I don't understand why it won't work either. But why don't you let bc calculate the number, then format it as a string using something like sed?
Code:
$ echo "(($initsize-$zipsize)/$initsize) * 100 " | bc -l 99.69790390114379084900 $ echo "(($initsize-$zipsize)/$initsize) * 100 " | bc -l | sed -e "s/\(\.[0-9]\).*/\1/g" 99.6 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|