
-----------------------------------
rar
Mon Sep 28, 2009 8:32 pm

Limiting Decimal Places
-----------------------------------
What is the syntax to limit a decimal to 2 decimal places?

Meaning if I'm reading output using "%d" or "%f" how do I modify it to limit to 2 decimal places?

-----------------------------------
bbi5291
Mon Sep 28, 2009 8:46 pm

Re: Limiting Decimal Places
-----------------------------------
What is the syntax to limit a decimal to 2 decimal places?

Meaning if I'm reading output using "%d" or "%f" how do I modify it to limit to 2 decimal places?

You don't read output, you write it.
%d is for integers, so I don't know what you mean by 2 decimal places.
You would use the format specifier %.2f to print a double to two decimal places.

-----------------------------------
DemonWasp
Mon Sep 28, 2009 8:47 pm

RE:Limiting Decimal Places
-----------------------------------
[url=http://en.wikipedia.org/wiki/Printf]You'd be surprised how much you can learn by typing "wiki printf" into your browser.

-----------------------------------
saltpro15
Tue Sep 29, 2009 12:19 pm

RE:Limiting Decimal Places
-----------------------------------
printf:


double n = 3.141592;
printf ("%.4f", n); //output to 3 decimals


cout:


double n = 3.141592;
cout.setf(ios_based::fixed);
cout.precision(4);
cout 