Getting VBScript to Correctly Interpret Number Formats Across Locales
April 2 2007
The Setup
You have a double formatted as a string with the decimal part separated from the whole part by a full-stop, e.g. "1234.9". Of course, not all cultures separate their digits the same way, and in fact, the site you're working on has a locale setting for a locale that uses a comma to separate the integer from the decimal, and vice-versa, e.g. German:"1.234,9" or French "1 234,9" style digit grouping. For the sake of argument, let's also say the decimal number is being parsed out of an XML file input by a US or UK user and that a full-stop is meant to separate the whole and fractional parts of the number.
The Problem
ASP/VBScript's clever and useful FormatNumber and FormatCurrency functions which solve oh-so-many related problems are no help:
SetLocale("de-DE")
'This is what we want (notice no quotes below)
FormatNumber(1234.9) '-> 1.234,90
'This is what we get (when using a string)
FormatNumber("1234.9") '-> 12.349,00
FormatCurrency("1234.9") ' -> 12.349,00 €
'ARGH!
When in a German locale, FormantNumber sees the string "1234.9", it assumes that the full-stop is merely a misplaced thousands separator and drops it. Though I was confused and angry at this behavior at first, I have come to see the sense that it makes (consider interpreting "1.234" in a German locale).
- Brandon
- ASP, Development, Hacks, Internationalization
- Comments
- 0 comments