Round up a number to so many decimal places

G

Gizmo

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.
 
J

Jakob Bieling

Gizmo said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top