That would work, but you have to understand that because of the limitations of the floating point format, not all decimal numbers will be expressible, thus when you truncate the number, you may see the last digit or two change in value. This is not problem with arnond's method, this is a limitation of the floating point system. For this reason, most programmer's don't truncate the value at all. They may limit the number of digits that print, but will try to retain full precision internally.
Main Topics
Browse All Topics





by: arnondPosted on 1998-10-18 at 05:51:24ID: 1175414
one way is to do:
double truncate (double D,int n)
{
long multiplied;
double to_return;
multiplied = (D*pow(10,n)); // save only the first n digits after the decimal point . (as a long integer).
to_return = (multiplied / (pow(10,n)); // return to the trtunced (original ) number.
return (to_return);
}
for example:
in truncate (0.123456789,5) you'll get:
multiplied = 12345
to_return = 0.12345