Getting VBScript to Correctly Interpret Number Formats Across Locales

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).

Something Like A Solution

Since in this particular case I "know" that The Right Thing to do is intepret the full-stop as a decimal separator, I just need to convince ASP to do so. One way to do that is to set the locale to a locale that inteprets the full-stop as a decimal separator like this:


SetLocale("en-US")
dbl = Cdbl("1234.9")
SetLocale("de-DE")
FormatNumber(dbl) '-> 1.234,90
FormatCurrency(dbl) ' -> 1.234,90 €
'Huzzah!

Of course there's no need to spatter the code with this sort of ridiculous locale switching all over the app. This sort of ugliness can be hidden easily enough:


Function intepretUSDouble(dbl)
  SetLocale("en-US")
  dbl = Cdbl(dbl)
  SetLocale("de-DE")
  intepretUSDouble = dbl
End Function

SetLocale("de-DE")
FormatNumber(intepretUSDouble("1234.9")) '-> 1.234,90
FormatCurrency(intepretUSDouble("1234.9")) ' -> 1.234,90 €

And yet I'm still not happy and have this aching suspicion that there's a way to do this without even introducing the locale-switching ugliness. Setting xml:lang on the document doesn't seem to convince the parser to do anything differently, sadly. The .NET Framework handles this by accepting a format provider to the parse method (which I suspect does much the same thing as this trick behind the scenes). Java at least throws an exception (which of course, is not an option here). Is there a better solution for classic ASP?


Share this post:deliciousdiggredditfurlgoogleyahoo
Related Posts
International Currencies in ASP
5½ Signs You Should Be Considering Refactoring
Web Project vs Local Project
Flash Remoting (via .NET v1.1) with VS 2005

Post new comment

  • The content of this field is kept private and will not be shown publicly.
    • Allowed HTML tags: <a> <em> <strong> <img> <code> <pre>
    • Lines and paragraphs break automatically.
    More information about formatting options
Archives
January, 2010 (2)
November, 2009 (1)
October, 2009 (1)
January, 2009 (1)
October, 2008 (1)
July, 2008 (1)
May, 2008 (3)
April, 2008 (1)
March, 2008 (1)
February, 2008 (1)
January, 2008 (1)
November, 2007 (2)
October, 2007 (1)
September, 2007 (2)
August, 2007 (3)
July, 2007 (3)
June, 2007 (2)
May, 2007 (4)
April, 2007 (4)
March, 2007 (4)
Tags
.NET ASP award awards Banner blog Campaign CMS Design Development DryJoys Flash FootJoy Forms Hacks Information Architecture Information Archtecture Interaction Internationalization Launch logo design Microsite Microsoft MITX Nomenclature PhizzPop process qa Usability Web Standards
Contributors
Brandon (8)
Denis (4)
Denise (21)
Jon (12)