If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Rounding numbers PLEASE

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-23-04, 14:25
hipolito hipolito is offline
Registered User
 
Join Date: Jun 2004
Posts: 15
Rounding numbers PLEASE

Hello all!

I have som numbers coming from somewhere with some strange format, such as, 19.9899992.
Well, I really need this number with 2 decimals, 19.99.

How do I round this number using CSHELL???

Thx.
Reply With Quote
  #2 (permalink)  
Old 06-23-04, 15:17
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
echo 'scale=2; 19.9899992 / 1' | bc
Reply With Quote
  #3 (permalink)  
Old 06-23-04, 16:43
hipolito hipolito is offline
Registered User
 
Join Date: Jun 2004
Posts: 15
Quote:
Originally Posted by vgersh99
echo 'scale=2; 19.9899992 / 1' | bc
Thx for your help,but I have tryed this and it does not round the number to 19.99 and yes it just cut the number like 19.98!!!

Please I need to round it.

Thx for your help.
Reply With Quote
  #4 (permalink)  
Old 06-23-04, 16:50
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
if you have /usr/bin/printf:

printf "%.2f\n" 19.9899992

if you don't:

echo 19.9899992 | nawk '{printf("%.2f\n", $0)}'
Reply With Quote
  #5 (permalink)  
Old 06-23-04, 17:25
Pat Phelan Pat Phelan is online now
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,613
Or if you don't have either printf or nawk but have perl, you could also use:
Code:
echo 19.9899992 | perl -e "$i = <>; printf \"%.2f\n\", $i"
-PatP
Reply With Quote
  #6 (permalink)  
Old 06-23-04, 17:38
hipolito hipolito is offline
Registered User
 
Join Date: Jun 2004
Posts: 15
Quote:
Originally Posted by vgersh99
if you have /usr/bin/printf:

printf "%.2f\n" 19.9899992

if you don't:

echo 19.9899992 | nawk '{printf("%.2f\n", $0)}'

Oh! yes, it works just fine, thx a lot man.
Reply With Quote
  #7 (permalink)  
Old 06-23-04, 23:30
srsjc srsjc is offline
Registered User
 
Join Date: Jun 2004
Posts: 20
Quote:
Originally Posted by vgersh99
echo 'scale=2; 19.9899992 / 1' | bc

Thx for your help,but I have tryed this and it does not round the number to 19.99 and yes it just cut the number like 19.98!!!

echo 'scale=0; 19.9899992 / 1' | bc
try with scale =0
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On