Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Money Format Number VB.NET

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.

like image 952
John Nuñez Avatar asked Dec 02 '22 23:12

John Nuñez


1 Answers

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)
like image 153
Dennis Traub Avatar answered Dec 18 '22 23:12

Dennis Traub