# rounding a float/double to nearest 1/10th

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

1. ### Shea MartinGuest

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

2. ### Shea MartinGuest

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

3. ### Gianni MarianiGuest

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
4. ### 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
5. ### Shea MartinGuest

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
6. ### Chris TheisGuest

"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
7. ### Chris TheisGuest

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
8. ### P.J. PlaugerGuest

"Steven C." <> wrote in message newsW1ob.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