• Join us

How to Normalize

To add comments, please log in or register
guimf
4413
guimf  

I would like to know, how to round a number and keep the last digit only 0 or 5.

For example:

Round (104,5) = 105

Round (102) = 100

Round (211,2) = 210 

Carl Schreiber
Moderator
8945
Carl Schreiber  

e.g. for a= 211.2 => 210 (=b): 

double b = 10.0*(double)((int)(a/10.0))
William Roeder
20344
William Roeder  
Guilherme Mendonca: I would like to know, how to round a number and keep the last digit only 0 or 5.
/** Round a double to a multiple of an amount.
 * @param[in] v   Value to round.
 * @param[in] to  The multiple.                                               */
double            round_down(    double v, double to){
                                                return to * MathFloor(v / to); }
/// Round a double to a multiple of an amount.
double            round_up(      double v, double to){
                                                return to * MathCeil( v / to); }
/// Round a double to a multiple of an amount.
double            round_nearest( double v, double to){
                                                return to * MathRound(v / to); }
////////////////////////////////////////////////////////////////////////////////
round_nearest(104.5, 5) // 105.0
round_nearest(102,   5) // 100.0
round_nearest(211.2, 5) // 210.0
round_nearest(1.23452, 5*_Point) // 1.23450
round_nearest(1.23456, 5*_Point) // 1.23455 (_Point=0.00001)
round_nearest(1.23458, 5*_Point) // 1.23460
guimf
4413
guimf  
whroeder1:

whroeder1:

Thank you!
To add comments, please log in or register