how do you format decimal places in C?
example: answer is 25.00000000
I want it formated to: 25.00 (two decimal places)
Format decimal places in C?
Like so:
float myNum = 25.25252525;
printf("My Number: %.2f", myNum);
...that will print 2 decimal places.
printf("My Number: %.4f", myNum);
...will print 4 places.
Good luck...
Reply:This is real easy. You remember the printf function, right? Well that function is short for "print formatted string", which allows one to format output in a variety of ways. What you need to do is use the %f escape sequence. This code has the format %a.b[efg], where [efg] indicates that this will work with %e, %f, and %g. a represents the number of digits on the right of the decimal point, and b represents the number on the left. These are called field widths. You can figure out the rest, I hope! 8^)%26gt;
Now, following the "teach a man to fish and he'll eat for a lifetime" tack, I'll point you to the most sacred text in C programming: "The C Programming Language" by Brian Kernighan and Dennis Ritchie. Not only does it provide the C programmer with an exhaustive language reference, complete with syntax and grammar definitions, but it also serves as a newbie tutorial to C.
Good luck!
Reply:%.2f if you are using printf()
garden state
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment