Format Decimal 2 Places - no trailing zeroes

ぐ巨炮叔叔 提交于 2019-12-07 13:44:46

问题


The format: #,# formats my number but it rounds my decimals up. I need to keep my decimals. Is there a format for that?

Thank you.


回答1:


#,#.###

Ad as many #s as needed.
To always show decimal places, use 0s instead.




回答2:


You can truncate decimal places with

(int)d

If you need two decimals you can write

(int)(100*d)/100.0

(int)(100*1.99999)/100.0 ==> 1.99



回答3:


If you are trying to display it as a string, try this...

yourVariable.ToString("{0:F2}");

This will format the string into a fixed point with two floating numbers.

The Microsoft Custom Numeric Format Strings and Microsoft Standard Numeric Format Strings pages have useful data you could refer to. :-)



来源:https://stackoverflow.com/questions/9707611/format-decimal-2-places-no-trailing-zeroes

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