Jump to content
  • Advertisement
Sign in to follow this  
fooman_69

c++ decimal rounding

This topic is 4926 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
To round down, just cast to an int:
float f = 1234.5678; int n = (int)f;
To round to the nearest number, add 0.5 and cast:
float f = 1234.5678; int n = (int)(f+0.5f);

Share this post


Link to post
Share on other sites
Or, the c library math.h provides:


double round(double);
float roundf(float);
// as well as
double ceil(double);
float ceilf(float);
double floor(double);
float floorf(float);


which do rounding, round up, and round down, respectively.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
To round down, just cast to an int:
float f = 1234.5678; int n = (int)f;
To round to the nearest number, add 0.5 and cast:
float f = 1234.5678; int n = (int)(f+0.5f);

Casting to int does not round down, it truncates (which would round down for positive numbers and round up for negatives). As well, casting to an integer value and back is a costly operation. Use floor for rounding down.

Edit:

Quote:
Original post by Telastyn
Or, the c library math.h provides:


double round(double);
float roundf(float);


There's round in math? I don't think so...

Share this post


Link to post
Share on other sites
how about if I want to round from 4 to 2 decimal places....
I knew about the math.h had the ceil and floor methods, but I'm not sure how to round without totally getting rid of all decimals.

Share this post


Link to post
Share on other sites
Rounds Number to nearest Precision from Anchor
The return value is always representable as (Integer * Precision) + Anchor.

To round to 5 decimal places, use pow(10, -5) as the precision and 0 as Anchor.
To round to the nearest 3 from 1 (so possible values are 1, 4, 7, etc) use precision 3 and anchor 1.

double Round(double p_Number, double p_Precision, double p_Anchor = 0)
{
if(p_Number < p_Anchor)
{
return ceil((p_Number - p_Anchor) / p_Precision) * p_Precision + p_Anchor;
}
else
{
return floor((p_Number - p_Anchor) / p_Precision) * p_Precision + p_Anchor;
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by fooman_69
how about if I want to round from 4 to 2 decimal places....
I knew about the math.h had the ceil and floor methods, but I'm not sure how to round without totally getting rid of all decimals.

You could multiply the number you are rounding by the base of the number system to the power of the number of places you wish to round off to. Then round that value, then divide back. Another option is you could add half of the accuracy, divide the starting value by the accuracy you need, round the result appropriately, then multiply that value by the accuracy you chose. Yet another option would be to take the starting value, add half of the accuracy and then subtract the result of an ::std::fmod (in cmath) of it by the accuracy you want. That's 3 different solutions.

Always keep in mind that you are dealing with floating point values so the exact rounded version of the value that you want may simply not be representable. Prefer using fixed point math if you absolutely must be exact.

Share this post


Link to post
Share on other sites
Quote:
Original post by Polymorphic OOP
There's round in math? I don't think so...



ROUND(3) NetBSD Library Functions Manual ROUND(3)

NAME
round, roundf - round to nearest integral value

LIBRARY
Math Library (libm, -lm)

SYNOPSIS
#include <math.h>

double
round(double x);

float
roundf(float x);

DESCRIPTION
The round() and roundf() functions return the nearest integral value to
x; if x lies halfway between two integral values, then these functions
return the integral value with the larger absolute value (i.e., they
round away from zero).

SEE ALSO
ceil(3), floor(3), ieee(3), math(3), rint(3)

STANDARDS
The round() function conforms to ISO/IEC 9899:1999 (``ISO C99'').



Relatively recent addition, but it's there.

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
Quote:
Original post by Polymorphic OOP
There's round in math? I don't think so...


*** Source Snippet Removed ***

Relatively recent addition, but it's there.

Are you sure it's standard? I don't believe so. Comeau doesn't recognize it in C99 mode though does in C90. Also, I don't see it mentioned in the C++ standard and comeau does not recognize it in C++ mode either.

Edit: I guess it is standard in C99 and comeau just doesn't recognize it for some reason.

Share this post


Link to post
Share on other sites
manpage says C99.

Nothing really definative comes up in my search, but these seem to indicate it is.

http://www.dinkumware.com/conform_c.html
http://members.aol.com/wantondeb/

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!