Round up a number to so many decimal places

Discussion in 'C++' started by Gizmo, Aug 31, 2003.

  1. Gizmo

    Gizmo Guest

    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.
     
    Gizmo, Aug 31, 2003
    #1
    1. Advertising

  2. "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)
     
    Jakob Bieling, Aug 31, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shez

    number of decimal places

    shez, Jan 20, 2005, in forum: C++
    Replies:
    4
    Views:
    6,755
    saurabh.unercat
    Feb 19, 2011
  2. Gaijinco
    Replies:
    29
    Views:
    2,904
    Anonymous 7843
    Sep 28, 2005
  3. Gaijinco
    Replies:
    27
    Views:
    1,077
    Anonymous 7843
    Sep 28, 2005
  4. harryos
    Replies:
    4
    Views:
    1,623
  5. Pat Maddox

    Round floats to N decimal places?

    Pat Maddox, Mar 22, 2006, in forum: Ruby
    Replies:
    4
    Views:
    181
Loading...

Share This Page