开发者

Clear trailing 0's on a double?

开发者 https://www.devze.com 2022-12-27 11:35 出处:网络
I have a double thats got a value of something like 0.50000 but I just want 0.5 - Is there any way to get rid of those trailing开发者_如何学运维 0\'s? :)In C,

I have a double thats got a value of something like 0.50000 but I just want 0.5 - Is there any way to get rid of those trailing开发者_如何学运维 0's? :)


In C,

printf("%g", 0.5000);

Note: (from GNU libc manual)

The %g and %G conversions print the argument in the style of %e or %E (respectively) if the exponent would be less than -4 or greater than or equal to the precision; otherwise they use the ‘%f’ style. A precision of 0, is taken as 1. Trailing zeros are removed from the fractional portion of the result and a decimal-point character appears only if it is followed by a digit.


standard c format statements.

NSLog(@" %.2f", .5000)
0

精彩评论

暂无评论...
验证码 换一张
取 消