views:

323

answers:

4

I have a float with the value of e.g 57.400002. I use sprintf_s to display the value on my GUI.

sprintf_s(xPosition, 19, "%f", xPositionValue);

How can I format the float so it displays as 57.40?

+8  A: 
sprintf_s(xPosition, 19, "%.2f", xPositionValue);

See http://www.cplusplus.com/reference/clibrary/cstdio/printf/ for more documentation on format codes.

JSBangs
+1 for the link to relevant documentation.
Mark Ransom
+4  A: 

sprintf_s(xPosition, 19, "%.2f", xPositionValue);

Dominic Rodger
Thanks works. :)
+3  A: 
sprintf_s(xPosition, 19, "%.2f", xPositionValue);

should do the trick doesn't it ?

Lliane
+1  A: 

Use the width and precision tags, just like printf

See http://www.cplusplus.com/reference/clibrary/cstdio/printf/

I think you would want the following:

sprintf_s(xPosition, 19, "%.2f", xPositionValue);

rnistuk