D
dcorbit
Dik said:For this kind of data, (int)ftrunc(f * 100 + 0.5) works just as well.
C99 has some fair level of sophistication in rounding.
ISO/IEC 9899:1999 (E) ©ISO/IEC
7.6.3 Rounding
1 The fegetround and fesetround functions provide control of rounding
direction modes.
7.6.3.1 The fegetround function
Synopsis
1 #include <fenv.h>
int fegetround(void);
Description
2 The fegetround function gets the current rounding direction.
Returns
3 The fegetround function returns the value of the rounding direction
macro representing the current rounding direction or a negative value
if there is no such rounding direction macro or the current rounding
direction is not determinable.
7.6.3.2 The fesetround function
Synopsis
1 #include <fenv.h>
int fesetround(int round);
Description
2 The fesetround function establishes the rounding direction
represented by its argument round. If the argument is not equal to the
value of a rounding direction macro, the rounding direction is not
changed.
Returns
3 The fesetround function returns a zero value if and only if the
argument is equal to a rounding direction macro (that is, if and only
if the requested rounding direction was established).
4 EXAMPLE Save, set, and restore the rounding direction. Report an
error and abort if setting the rounding direction fails.
#include <fenv.h>
#include <assert.h>
void f(int round_dir)
{
#pragma STDC FENV_ACCESS ON
int save_round;
int setround_ok;
save_round = fegetround();
setround_ok = fesetround(round_dir);
assert(setround_ok == 0);
/* ... */
fesetround(save_round);
/* ... */
}
7.12.9.6 The round functions
Synopsis
1 #include <math.h>
double round(double x);
float roundf(float x);
long double roundl(long double x);
Description
2 The round functions round their argument to the nearest integer value
in floating-point format, rounding halfway cases away from zero,
regardless of the current rounding direction.
Returns
3 The round functions return the rounded integer value.
7.12.9.7 The lround and llround functions
Synopsis
1 #include <math.h>
long int lround(double x);
long int lroundf(float x);
long int lroundl(long double x);
long long int llround(double x);
long long int llroundf(float x);
long long int llroundl(long double x);
Description
2 The lround and llround functions round their argument to the nearest
integer value, rounding halfway cases away from zero, regardless of the
current rounding direction. If the rounded value is outside the range
of the return type, the numeric result is unspecified. A range error
may occur if the magnitude of x is too large.
Returns
3 The lround and llround functions return the rounded integer value.
7.12.9.8 The trunc functions
Synopsis
1 #include <math.h>
double trunc(double x);
float truncf(float x);
long double truncl(long double x);
Description
2 The trunc functions round their argument to the integer value, in
floating format, nearest to but no larger in magnitude than the
argument.
Returns
3 The trunc functions return the truncated integer value.
On the other hand, I surprised that they did not at least model the
ANSI/ISO SQL rounding function, which is a lot more likely to be what
people need.