By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,030 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,030 IT Pros & Developers. It's quick & easy.

Round up a number to so many decimal places

P: n/a
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.
Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Gizmo" <sc***********@hotmail.com> wrote in message
news:bi**********@newsg4.svr.pol.co.uk...
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)
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.