• 10
• 12
• 12
• 14
• 17

# A c problem.precision on printf()

This topic is 2740 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi,guys!I met a problem
you know,printf("%.1f",123.45); output 123.5
but how to control the result 123.50?
printf("%.1f0",123.45); is just a idea,any other ways?thx!

##### Share on other sites
Why would you want to output deliberately imprecise values?

##### Share on other sites
A terrible problem post by my professor!

##### Share on other sites
i think the best solution is the one you posted... anyway, this is a strange problem, can you give us any more detail that would explain why you would want to do that?

##### Share on other sites
yeah,I had ask on irc but hadn't found any idea yet,it's a lovely problem post by my lovely professor.

##### Share on other sites
Quote:
 Original post by SiCraneWhy would you want to output deliberately imprecise values?

Perhaps because the result is from a calculation using measured values with limited precision. For example, 11.5 * 10.9 = 125.35 => 125.40. On the other hand, the zero suggests an additional significant figure which carries forward the imprecision. My understanding of this is a bit rusty. I'll have to investigate further.

##### Share on other sites
Hope this helps.

int _tmain(int argc, _TCHAR* argv[]){	float number;	printf("Input a float: ");	cin >> number;	printf("Round to 1/20: %.2f", floor(number * 20 + 1) / 20);}

P.S: First post on Gamedev :P.

##### Share on other sites
Quote:
 Original post by LessBreadOn the other hand, the zero suggests an additional significant figure which carries forward the imprecision. My understanding of this is a bit rusty. I'll have to investigate further.

Common use of significant figures implies that additional zeros after a decimal point are significant. Rounding at the tenths position and placing a zero at the hundredths position would be deliberately implying a different degree of precision than being displayed.

##### Share on other sites
Yes. I went and checked a book. The appended zero would become a significant figure.

Are there any other explanations for why someone would want to do that (outside of dishonest reasons)? Is rounding to whole fractions the only one?