Click to See Complete Forum and Search --> : another simple question


July 28th, 1999, 04:01 AM
I have this code:

float myFloat = 100.520920
CString myString;
myString.Format("%f",myFloat);

I want the string to display 100.52 (limit to two decimal places)
how can I do this?

Martin Speiser
July 28th, 1999, 04:20 AM
Hi,

myString.Format("%.2f",myFloat);

The number after the dot is the count of the decimal places. Format ist calling sprintf, it will be rounded. 0.4999 will be 0, 0.5 will be 1.

BTW, if you're writing a "real" application (not just for learning)you need to take care of the regional settings. Some countries use a comma as decimal point.

Martin