Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Proper DataType for Currency 1

Status
Not open for further replies.

khowell78737

Programmer
Nov 18, 2003
22
US
would i store a currency value as a decimal, single or double?

thanks!
Kevin
 
I suppose the answer depends on memory constraints of the machine the program runs on. As this used to be an issue years ago, programmers were perhaps inclined to use a data type that didn't utilise much memeory. However, this day and age that doesn't seem to be an issue - therefore the choice, at the end of the day, is up to the programmer.

Personally, I would store the value as Decimal
 
I would use System.Decimal.

To create a currency value with 2 decimal places (like in US currency), you have to first start off with the value specified in cents, not dollars & cents:
Code:
Imports System.Globalization

Dim decNum As New Decimal(345, 0, 0, False, 2)
Dim cc As New CultureInfo("en-US")
Console.WriteLine(decNum.ToString("C", cc.NumberFormat))
This will write $3.45 to the command window.

Of course, the Decimal just contains a value, and doesn't know $ from £, so you'll need to store which currency it's in in a separate variable. I recommend you create a new class called IntlMoney which will encapsulate both the value, and the currency it's in.

I don't know why the framework doesn't have something like that.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top