问题
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 0
s 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