rounding a float/double to nearest 1/10th

S

Shea Martin

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
 
S

Shea Martin

Shea said:
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
 
G

Gianni Mariani

Shea said:
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 );
 
O

Oliver S.

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.
 
S

Shea Martin

Gianni said:
Shea said:
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
 
C

Chris Theis

[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
 
C

Chris Theis

Chris Theis said:
news:[email protected]... [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

P.J. Plauger

Steven C. said:
Gianni said:
Shea said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top