I’m looking for a good way to parse numbers entered into a listbox cell, converting them into doubles. The numbers can be in either American or European format (so the decimal point and thousands separators are different). In most cases I do know which system is being used, but my mind has wandered off today and it suddenly seems complicated to get this text back to a double.
The most difficult seems to be to remove the thousands separators, after which you want to turn the European comma into a dot, and your are set to use CDbl.
So if I know it’s european format, a simple ReplaceAll(".", “”) followed by ReplaceAll(",", “.”) should do it, right? Assuming that a period is the thousands separator.
I just realized the easy way to find out the replacements is to make a string using FORMAT with thousands and see what Xojo uses, then do the same thing with a decimal.
Are there any other tricky things I should know about, or is it really that straightforward?
[quote=285656:@John McKernon]I’m looking for a good way to parse numbers entered into a listbox cell, converting them into doubles. The numbers can be in either American or European format (so the decimal point and thousands separators are different). In most cases I do know which system is being used, but my mind has wandered off today and it suddenly seems complicated to get this text back to a double.
In general (France, Germany, Spain, Italy… ), the thousands separator is coma. BUT in Switzerland it is single quote. There may be other local peculiarities.
FUNCTION cleanNumber(s:String) as String
var ThouSep = replaceAll(format("9,999"),"9","") // should return comma for US and dot for Europe?
var DecSep = replaceAll(format("9.99"),"9","") // should return dot for US and comma for Europe?
s=replaceAll(replaceall(s,decsep,"."),,thousep,"")
// should remove thousand seps and change decimal indicator to dot
return s
END FUNCTION
That looks pretty much like what I was figuring I’d have to do, I’m putting together a test project to send my european beta testers and see if we’ve finally got it right.
[quote=285673:@John McKernon]That looks pretty much like what I was figuring I’d have to do, I’m putting together a test project to send my european beta testers and see if we’ve finally got it right.
[/quote]
NO Michel was wrong
Just use CDBL which behaves correctly
Failing that use Double.Fromtext with a specific locale
Either one should work