Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Limiting Decimal Places
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
rar




PostPosted: 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?
Sponsor
Sponsor
Sponsor
sponsor
bbi5291




PostPosted: 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.
DemonWasp




PostPosted: 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.
saltpro15




PostPosted: 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;
rar




PostPosted: 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!
bbi5291




PostPosted: 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?
Kharybdis




PostPosted: Tue Sep 29, 2009 5:20 pm   Post subject: RE:Limiting Decimal Places

Probably..
rar




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: