Computer Science Canada

Limiting Decimal Places

Author:  rar [ Mon Sep 28, 2009 8:32 pm ]
Post subject:  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?

Author:  bbi5291 [ Mon Sep 28, 2009 8:46 pm ]
Post subject:  Re: Limiting Decimal Places

rar @ Mon Sep 28, 2009 8:32 pm wrote:
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.

Author:  DemonWasp [ Mon Sep 28, 2009 8:47 pm ]
Post subject:  RE:Limiting Decimal Places

You'd be surprised how much you can learn by typing "wiki printf" into your browser.

Author:  saltpro15 [ Tue Sep 29, 2009 12:19 pm ]
Post subject:  RE:Limiting Decimal Places

printf:

c++:

double n = 3.141592;
printf ("%.4f", n); //output to 3 decimals


cout:

c++:

double n = 3.141592;
cout.setf(ios_based::fixed);
cout.precision(4);
cout << n << endl;

Author:  rar [ Tue Sep 29, 2009 2:05 pm ]
Post subject:  Re: Limiting Decimal Places

bbi5291 @ Mon Sep 28, 2009 8:46 pm wrote:
rar @ Mon Sep 28, 2009 8:32 pm wrote:
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.


Thanks for commenting on the obvious technical errors in my post.

Also, thanks for your help!

Author:  bbi5291 [ Tue Sep 29, 2009 2:57 pm ]
Post subject:  Re: Limiting Decimal Places

Quote:

Thanks for commenting on the obvious technical errors in my post.

Is that sarcastic?

Author:  Kharybdis [ Tue Sep 29, 2009 5:20 pm ]
Post subject:  RE:Limiting Decimal Places

Probably..

Author:  rar [ Wed Sep 30, 2009 1:01 pm ]
Post subject:  Re: Limiting Decimal Places

bbi5291 @ Tue Sep 29, 2009 2:57 pm wrote:
Quote:

Thanks for commenting on the obvious technical errors in my post.

Is that sarcastic?


A bit of sarcasm, yes, but only because you were quite right and I looked like an idiot. Smile


: