Wednesday, January 2, 2013

Format, CStr and the Web

Since this issue keeps coming up, and the reason isn't terribly obvious in all cases, I want to point out a subtle problem with numbers when dealing with web projects.

If you are at all familiar with the Format and CStr commands, you know that they convert a number into a string using the localization of the computer that they are running on. For example:

Dim s as String = Format(1.23,"0.00")

// United States, s = "1.23"
// Europe, s = "1,23"

The same goes for CStr.

There are two issues with this when dealing with the web:

1. Remember that the code that runs these methods is on the server! The localization information is not pulled from the individual browsers, so you'll need to take that into account when displaying numbers.

2. If you're sending JavaScript commands to the browser that include numbers, like if you're using our WebSDK, JavaScript requires numbers to be in the U.S. English format (using "." as the decimal separator).

To get around this, there is a special version of the Str command that applies the same kind of format string, but does not apply the region format. Using the example from above:

Dim s as String = Str(1.23,"0.00")

// United States: s = "1.23"
// Europe: s = "1.23"

No comments: