A c problem.precision on printf()

Started by
9 comments, last by LancerSolurus 13 years, 6 months ago
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!
Advertisement
Why would you want to output deliberately imprecise values?
A terrible problem post by my professor!
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?
------------------------------
Join the revolution and get a free donut!
yeah,I had ask on irc but hadn't found any idea yet,it's a lovely problem post by my lovely professor.
Quote:Original post by SiCrane
Why 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.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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.
Quote:Original post by LessBread
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.

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.
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?
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
well,I choose the way I posted finally,it's boring.

This topic is closed to new replies.

Advertisement