简体   繁体   English

尽管四舍五入到两位小数,但仅输出一个小数位

[英]Only single decimal place output despite rounding to 2 decimal places

apologies if this has been covered before, I've had a search but think my search terms must be inaccurate. 抱歉,如果以前已经解决过这个问题,我已经进行了搜索,但认为我的搜索词一定不准确。

Anyway, I am working on a project that included a lot of monetary calculations. 无论如何,我正在从事一个包含大量金钱计算的项目。 I'm using decimal variable types. 我正在使用十进制变量类型。

I'm seeing alot of my figures coming out as 3.5 instead of 3.50, which is fine in terms of calculations but when displayed its shown as £3.5 and not £3.50. 我看到很多数字是3.5而不是3.50,这在计算方面是可以的,但是当显示为£3.5而不是£3.50时。

Is there a way I can force it to display the 2nd decimal place, even if its just a 0? 有没有办法强制我显示小数点后第二位,即使它只是0?

Thanks 谢谢

You just have to use the currency format specifier which uses the number of decimal places which NumberFormatInfo.CurrentInfo.CurrencyDecimalDigits returns. 您只需要使用货币格式说明符,该说明符使用NumberFormatInfo.CurrentInfo.CurrencyDecimalDigits返回的小数位数。

Dim value As Decimal = 0D
Dim output = value.ToString("C")  ' 0,00 € for me in germany '

If you want to force a specific culture use the overload: 如果要强制使用特定区域性,请使用重载:

Dim output = value.ToString("C", New CultureInfo("en-GB")) ' £0.00 '

The Currency ("C") Format Specifier 货币(“ C”)格式说明符

You can also force n-number of decimal places by using the "N" format specifier : 您还可以使用"N"格式说明符来强制使用n个小数位:

Dim output As String = value.ToString("N2")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM