John
John

Reputation: 785

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

Upvotes: 0

Views: 67

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460108

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")

Upvotes: 2

Related Questions