Velocity Reviews > C++ > rounding a float/double to nearest 1/10th

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

Shea Martin
Guest
Posts: n/a

 10-28-2003
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
Guest
Posts: n/a

 10-28-2003
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

Gianni Mariani
Guest
Posts: n/a

 10-28-2003
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 );

Oliver S.
Guest
Posts: n/a

 10-28-2003
> 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.

Shea Martin
Guest
Posts: n/a

 10-28-2003
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

Chris Theis
Guest
Posts: n/a

 10-29-2003

"Gianni Mariani" <(E-Mail Removed)> wrote in message
news:bnmr3c\$(E-Mail Removed)...
[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
Guest
Posts: n/a

 10-29-2003

"Chris Theis" <(E-Mail Removed)> wrote in message
news:bno308\$mcd\$(E-Mail Removed)...
>
> "Gianni Mariani" <(E-Mail Removed)> wrote in message
> news:bnmr3c\$(E-Mail Removed)...

[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

P.J. Plauger
Guest
Posts: n/a

 10-30-2003
"Steven C." <(E-Mail Removed)> wrote in message newsW1ob.6478\$(E-Mail Removed)...

> "Shea Martin" <(E-Mail Removed)> 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

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Mr. Ken C++ 2 05-25-2006 12:54 PM =?ISO-8859-1?Q?G=F6ran_Andersson?= ASP .Net 1 04-28-2006 08:29 AM chaz ASP .Net 0 04-27-2006 01:25 PM Greg Young [MVP] ASP .Net 0 04-27-2006 12:09 PM Derek Basch Python 8 02-28-2006 11:54 PM