Advertisement

Floating Point Truncating

Started by March 27, 2001 10:11 PM
3 comments, last by makezImage 23 years, 10 months ago
I''m calculating floating point numbers, except I only want the number to be only two decimal places. How do I truncate the remaining decimal places? Ex. 12.375 -> 12.37 200.91235 -> 200.91 Thanks in advance
char szTemp;
sprintf(szTemp, "%.2f", 12.375);

will make a string with 12.38 in it (i think, might be 12.37)

Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
Thanks, but I want to put the number back into the floating point variable, and not into a string. I suppose I could do what you have, and then use a string to float function to do the conversion, but isn''t there a more direct route? I''m just assuming there would be...
Basic method involved to round number to 1/100th

float num = (float)(12.96453);
int num1 = (int)num;
num = num - num1;
num = (float)(int)(num * 100);
num = num1 + (num / 100);
even simple method to truncate

num = ((float)(int)(num * 100))/100;

This topic is closed to new replies.

Advertisement