Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Round up a number to so many decimal places

Reply
Thread Tools

Round up a number to so many decimal places

 
 
Gizmo
Guest
Posts: n/a
 
      08-31-2003
Hi there i was wondering if any one new to a function that rounds up a float
to so many decimal places. I have a number in bytes and converting it to
mb's and gb's but once its converted i need to be able to do it to 2 decimal
places.

Thanks for any help

Scott.


 
Reply With Quote
 
 
 
 
Jakob Bieling
Guest
Posts: n/a
 
      08-31-2003
"Gizmo" <> wrote in message
news:bist4t$4fn$...
> Hi there i was wondering if any one new to a function that rounds up a

float
> to so many decimal places. I have a number in bytes and converting it to
> mb's and gb's but once its converted i need to be able to do it to 2

decimal
> places.



An admittingly rather clumsy approach would be to multiply your
float/double by 10^x, convert to long, convert back to float/double and
divide by 10^x. 'x' is in both cases the number of wanted decimal places.

Note that this works only for little x, since otherwise you might
quickly encounter an overflow. In your case I doubt that will be the case.

Also, I am not too sure if you really need to round the numbers. If you
are converting to MB's and GB's so you can display the value on screen
somehow, I would suggest you leave the floats/doubles unrounded and let the
conversion function (ie. int to string) do the work. Like yo can do with
(s)printf:

sprintf (mystring, "You have %.02f GB left", fGB);

hth
--
jb

(replace y with x if you want to reply by e-mail)


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
number of decimal places shez C++ 4 02-19-2011 06:09 AM
number of decimal places in a double harryos Java 4 11-17-2009 09:47 PM
Round floats to N decimal places? Pat Maddox Ruby 4 03-27-2006 09:06 PM
How to tell if a number has decimal places different than 0 Gaijinco C++ 27 09-28-2005 07:58 PM
How to tell if a number has decimal places different than 0 Gaijinco C Programming 29 09-28-2005 07:58 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57