Original Post
Can anyone point me to a place that shows me how to do this?
double round(double);float roundf(float);// as well asdouble ceil(double);float ceilf(float);double floor(double);float floorf(float);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);
Quote:
Original post by Telastyn
Or, the c library math.h provides:
double round(double);
float roundf(float);<!--QUOTE-->
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; }}
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.
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 valueLIBRARY 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'').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.
#include <iterator>#include <algorithm>#include <string>#include <vector>#include <iostream>/* * Templated round function * * @author: Danushka "silvermace" Abeysuriya * @template param: T, data type and return type * @template param: Ndp, the number of decimal places to round to * @param: val, the value to be rounded * @returns: rounded val to Ndp decimal places of type T */template< typename T, const int Ndp >T round( const T val ){ T raised = pow( 10, (double)( abs(Ndp) ) ); T temp = val * raised; T rounded = static_cast<T>( floor( temp ) ); if( temp - rounded >= (T)0.5 ) { rounded = static_cast<T>( ceil( temp ) ); } return static_cast<T>(rounded / raised);}/* * unit tester function */int main(){ std::vector<float> test; test.push_back( round<float, 1>( 3.32645f ) ); test.push_back( round<float, 2>( 3.32645f ) ); test.push_back( round<float, 3>( 3.32645f ) ); test.push_back( round<float, 4>( 3.32645f ) ); test.push_back( round<float, 5>( 3.32645f ) ); std::copy( test.begin(), test.end(), std::ostream_iterator< float >(std::cout, "\n") ); return 0;}3.33.333.3263.32653.32645This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more