简体   繁体   中英

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.

Is there a way I can force it to display the 2nd decimal place, even if its just a 0?

Thanks

You just have to use the currency format specifier which uses the number of decimal places which NumberFormatInfo.CurrentInfo.CurrencyDecimalDigits returns.

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

You can also force n-number of decimal places by using the "N" format specifier :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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