Money Format Number VB.NET

John Nuñez picture John Nuñez · Feb 13, 2012 · Viewed 69.2k times · Source

I'm trying to convert a mathematical result of money format example:

Dim num1 As Integer = 2000
Dim num2 As Integer = 500

msgbox(cDbl(num1 + num2))

It only returns 2500, which I need to return my 2,500.00 if anyone has any idea how I would be very helpful thanks.

Answer

Dennis Traub picture Dennis Traub · Feb 13, 2012

First, you should use Decimal instead of Double when handling monetary values. Double has some rounding issues.

Second, you can use string formatting:

Dim num1 As Integer = 2000
Dim num2 As Integer = 500
Diml value As Decimal = CDec(num1 + num2)
Dim formattedValue As String = String.Format("{0:n}", value)

msgbox(formattedValue)