The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-10-2006
noodlesoup noodlesoup is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 13
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?
  #2 (permalink)  
Old 09-10-2006
nathan nathan is offline VIP Member  
Supporter
  
 

Join Date: Jul 2006
Posts: 156
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
  #3 (permalink)  
Old 09-10-2006
noodlesoup noodlesoup is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 13
yeah it's gotten to me.
I changed the equation to this (same thing just rearranged) and it works. strange!

(($initsize-$zipsize)*100/$initsize)
  #4 (permalink)  
Old 09-11-2006
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
I'm not sure why you find it strange...
Code:
$ bc
bc 1.03 (Nov 2, 1994)
Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
scale=9
(1566720-4733) / 1566720
.996979039
scale=1
(1566720-4733) / 1566720
.9
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:32 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0