rounding a float/double to nearest 1/10th

Discussion in 'C++' started by Shea Martin, Oct 28, 2003.

  1. Shea Martin

    Shea Martin Guest

    Any one have a better/simpler method for rounding a float to the nearest
    1/10th? This is currently what I am using, and there must be a better
    way, or perhaps a canned method that I am not aware of.

    double z = atof(arg[1]);
    z = z*100.0;
    int zi = (int)floor((double)z);
    int ri = zi%10;
    zi -= ri;
    zi += ( ri < 5 ) ? 0 : 10;
    z = (double)zi/(double)100;

    NOTE: originally the doubles were floats, but 1.5*100 = 149.9999 when
    using floats, correct answer given when doubles used.

    Any suggestions would be great thank.

    ~S
    Shea Martin, Oct 28, 2003
    #1
    1. Advertising

  2. Shea Martin

    Shea Martin Guest

    Shea Martin wrote:
    > Any one have a better/simpler method for rounding a float to the nearest
    > 1/10th? This is currently what I am using, and there must be a better
    > way, or perhaps a canned method that I am not aware of.
    >
    > double z = atof(arg[1]);
    > z = z*100.0;
    > int zi = (int)floor((double)z);
    > int ri = zi%10;
    > zi -= ri;
    > zi += ( ri < 5 ) ? 0 : 10;
    > z = (double)zi/(double)100;
    >
    > NOTE: originally the doubles were floats, but 1.5*100 = 149.9999 when
    > using floats, correct answer given when doubles used.
    >
    > Any suggestions would be great thank.
    >
    > ~S
    >


    float z2 = atof(arg[1]);
    z2 = (float)floor(z2*10+0.5)/10;

    I thinks this is a lot better.

    ~S
    Shea Martin, Oct 28, 2003
    #2
    1. Advertising

  3. Shea Martin wrote:
    > Any one have a better/simpler method for rounding a float to the nearest
    > 1/10th? This is currently what I am using, and there must be a better
    > way, or perhaps a canned method that I am not aware of.
    >
    > double z = atof(arg[1]);
    > z = z*100.0;
    > int zi = (int)floor((double)z);
    > int ri = zi%10;
    > zi -= ri;
    > zi += ( ri < 5 ) ? 0 : 10;
    > z = (double)zi/(double)100;
    >
    > NOTE: originally the doubles were floats, but 1.5*100 = 149.9999 when
    > using floats, correct answer given when doubles used.



    z = 0.1 * round( z * 10.0 );
    Gianni Mariani, Oct 28, 2003
    #3
  4. Shea Martin

    Oliver S. Guest

    > float z2 = atof(arg[1]);
    > z2 = (float)floor(z2*10+0.5)/10;


    better

    z2 = (float)(floor( z2 * 10 + 0.5 ) / 10)

    So the value gets chopped to a float (if at all) after the division.
    Oliver S., Oct 28, 2003
    #4
  5. Shea Martin

    Shea Martin Guest

    Gianni Mariani wrote:
    > Shea Martin wrote:
    >
    >> Any one have a better/simpler method for rounding a float to the
    >> nearest 1/10th? This is currently what I am using, and there must be
    >> a better way, or perhaps a canned method that I am not aware of.
    >>
    >> double z = atof(arg[1]);
    >> z = z*100.0;
    >> int zi = (int)floor((double)z);
    >> int ri = zi%10;
    >> zi -= ri;
    >> zi += ( ri < 5 ) ? 0 : 10;
    >> z = (double)zi/(double)100;
    >>
    >> NOTE: originally the doubles were floats, but 1.5*100 = 149.9999 when
    >> using floats, correct answer given when doubles used.

    >
    >
    >
    > z = 0.1 * round( z * 10.0 );
    >

    Which header do I need to get round? It does not seem to be in math.h
    on Solaris 9.

    Thanks,

    ~S
    Shea Martin, Oct 28, 2003
    #5
  6. Shea Martin

    Chris Theis Guest

    "Gianni Mariani" <> wrote in message
    news:bnmr3c$...
    [SNIP]
    > z = 0.1 * round( z * 10.0 );
    >


    Somehow I missed that round() was a standard function. I'd be happy if you
    could point out where I can find it, so that I can get rid of my own
    solution.

    I usually use the following approach:

    double Round( double Value, int Digits )
    {
    if( Value > 0.0 )
    return ( (long)( Value * Faktor + 0.5 ) ) / pow( 10.0, Digits);

    return ( (long)( Value * Faktor - 0.5 ) ) / pow( 10.0, Digits);
    }

    In case of common values for Digits one could use a table instead of
    calculating the factor with pow() as it is a rather slow function.

    Regards
    Chris
    Chris Theis, Oct 29, 2003
    #6
  7. Shea Martin

    Chris Theis Guest

    Correction Re: rounding a float/double to nearest 1/10th

    "Chris Theis" <> wrote in message
    news:bno308$mcd$...
    >
    > "Gianni Mariani" <> wrote in message
    > news:bnmr3c$...

    [SNIP]
    > double Round( double Value, int Digits )
    > {
    > if( Value > 0.0 )
    > return ( (long)( Value * pow( 10.0, Digits) + 0.5 ) ) / pow( 10.0,

    Digits);

    Sorry, this should of course be
    if( Value > 0.0 )
    return ( (long)( Value * pow( 10.0, Digits) + 0.5 ) ) / pow( 10.0,
    Digits);

    return ( (long)( Value * pow( 10.0, Digits) - 0.5 ) ) / pow( 10.0,
    Digits);

    > }
    >


    It's obviously too early for me :)

    Chris
    Chris Theis, Oct 29, 2003
    #7
  8. Shea Martin

    P.J. Plauger Guest

    "Steven C." <> wrote in message news:eek:W1ob.6478$...

    > "Shea Martin" <> wrote in message
    > news:fNCnb.9864$f7.536027@localhost...
    > Gianni Mariani wrote:
    > > Shea Martin wrote:
    > >
    > >> Any one have a better/simpler method for rounding a float to the
    > >> nearest 1/10th? This is currently what I am using, and there must be
    > >> a better way, or perhaps a canned method that I am not aware of.
    > >>
    > >> double z = atof(arg[1]);
    > >> z = z*100.0;
    > >> int zi = (int)floor((double)z);
    > >> int ri = zi%10;
    > >> zi -= ri;
    > >> zi += ( ri < 5 ) ? 0 : 10;
    > >> z = (double)zi/(double)100;
    > >>
    > >> NOTE: originally the doubles were floats, but 1.5*100 = 149.9999 when
    > >> using floats, correct answer given when doubles used.

    > >
    > >
    > >
    > > z = 0.1 * round( z * 10.0 );
    > >

    > Which header do I need to get round? It does not seem to be in math.h
    > on Solaris 9.


    You need a version of math.h that better conforms to C99. We offer
    such a library, but it's an extra-cost item.

    P.J. Plauger
    Dinkumware, Ltd.
    http://www.dinkumware.com
    P.J. Plauger, Oct 30, 2003
    #8
    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. Greg Young [MVP]

    Re: rounding values to the nearest 5 or 10

    Greg Young [MVP], Apr 27, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    472
    Greg Young [MVP]
    Apr 27, 2006
  2. chaz
    Replies:
    0
    Views:
    385
  3. =?ISO-8859-1?Q?G=F6ran_Andersson?=

    Re: rounding values to the nearest 5 or 10

    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Apr 28, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    8,222
    Milsnips
    Apr 28, 2006
  4. bd
    Replies:
    0
    Views:
    624
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,540
    James Kanze
    Oct 8, 2009
Loading...

Share This Page