John
John

Reputation: 3

VB.Net Currency Converter - Is there an easier way?

Just picked programming up as a hobby, and am very bad. I am trying to build a basic currency converter from the book I have.

My question: Is there is a better way than taking the user input (lets say 5 dollars), convert the text string to a double, multiply by the rate, and then convert it back to a string in order to display it in the other textbox? I mainly ask because the textbook hasn't said how to convert double to string yet, and I found it online but I feel like I'm missing something.

Thanks

Upvotes: 0

Views: 1210

Answers (1)

Joel Coehoorn
Joel Coehoorn

Reputation: 415665

Whenever you're working with money, use the Decimal type. It works just like Double in every way, except that Double is not 100% accurate for certain arithmetic operations. Decimal is a bit slower, but it's accurate, and when you're working with money, the accuracy is almost always more important.

the textbook hasn't said how to convert double to string yet

That's easy:

'The "D" at the end is a special code that means it's a Decimal literal value
Dim d As Decimal = 12345.67D 
Dim s As String = d.ToString()

Going the other direction isn't much harder:

Dim d2 As Decimal = Decimal.Parse(s)

Upvotes: 2

Related Questions