Clear trailing 0's on a double?

a 夏天 提交于 2019-12-19 22:01:40

问题


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? :)


回答1:


standard c format statements.

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



回答2:


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.



来源:https://stackoverflow.com/questions/2641108/clear-trailing-0s-on-a-double

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!