Hi,
I would like to store money as Integer64 in an SQLite database to avoid potential rounding errors. I do not want to use the Currency data type.
In the app, the user may change currencies depending on the record being created, so I cannot rely the users locale for formatting. The app will only support currencies with 2 decimal places.
- Since I can’t rely on locale, what’s the best way to modify my CurrencyVal function so that it respects when a decimal point or comma is used as a decimal separator?
- Is it ok to give the Format function an Int64?
- Why does my current code format Euros and Pounds as .00000 and 0£0.00 for a value of 0? Yet the formats for other territories work fine. Is this a bug?
- Is there a better way to do this? I was wondering if my assumptions in my code are correct and would greatly appreciate any critiquing.
Thank you
Example from the LostFocus
event of a TextField:
me.text = CurrencyFormat(me.text.CurrencyVal, theCurrencyCode)
[code]Function CurrencyVal(Extends s As String) As Int64
// Takes a string and gets rid of any character that’s not a number, comma or decimal point.
// This includes removing spaces as some currencies use a space as the thousands separator.
// Return the result passed through CDbl since CDbl is international-savvy and will recognize
// which character is the decimal separator and if that separator is a decimal point or a comma.
// CDbl converts the string based on the user’s local numeric format settings.
// EDIT: Cant rely on CDbl
dim i as Integer
dim t as String
Dim ch As String
for i = 1 to len( s )
ch = Mid(s, i, 1)
if isnumeric( ch ) _
or ( ch = “.” ) _
or ( ch = “,” ) _
then t = t + ch
next i
// CDbl will return just the numeric form of the currency string with
// decimal separator in place and any thousands separators removed.
// Multiply by 1000 to remove decimal and return an Integer64.
return t.CDbl * 1000
End Function[/code]
[code]Function CurrencyFormat(amt as Int64, code as String) As String
// Divide by 1000 to get back the decimal
amt = amt / 1000
select case code
case kCurCodeUSD // US, Canada, Canada French
// Decimal Point: . Example: 1,234.12
// Local currency: $ International currency: USD
return Format( amt, kCurFormatUSD )
case kCurCodeAUD // Australia
// Decimal Point: . Example: 1 234.12
// Local currency: $ International currency: AUD
return Format( amt, kCurFormatAUD )
case kCurCodeEUR // Austria, Belgium French, France, Germany, Italy, Belgium Dutch, Netherlands, Spain, Ireland
// Decimal Point: . Example: 1,234.12
// Local currency: International currency: EUR
return Format( amt, kCurFormatEURO )
case kCurCodeCHF // Switzerland
// Decimal Point: , Example: 1.234,12
// Local currency: SF International currency: CHF
return Format( amt, kCurFormatCHF )
case kCurCodeGBP // UK
// Decimal Point: . Example: 1,234.123
// Local currency: ? International currency: GBP
return Format( amt, kCurFormatGBP )
else
return Format( amt, “###,##0.00;-###,##0.00;###,##0.00” )
end select
End Function[/code]
Const kCurFormatUSD = "\\$###,##0.00;-\\$###,##0.00;\\$###,##0.00"
Const kCurFormatAUD = "\\A\\U\\D\\ ###\\ ##0.00;\\A\\U\\D\\ -###\\ ##0.00;\\A\\U\\D\\ ###\\ ##0.00"
Const kCurFormatEURO = "\\###,##0.00;-\\###,##0.00;\\###,##0.00"
Const kCurFormatCHF = "\\C\\H\\F\\ ###.##0,00;\\C\\H\\F\\ -###.##0,00;\\C\\H\\F\\ ###.##0,00"
Const kCurFormatGBP = "\\£###,##0.00;-\\£###,##0.00;\\£###,##0.00"