Home > Programming > Format versus localization

Format versus localization

As fun as localization can be in .NET, sometimes you can struggle with it. I actually pity the American developer who will most likely code in something close to the Invariant culture and not know how un-portable his application will be.

For example, some (most) cultures use a decimal point, but others (like mine) use a decimal comma. So when you’re parsing an entry that’s in one culture (with decimal point), but your program runs another (with decimal comma), you get an error.

What you should do is define a format based on the input, and use that:

private static NumberFormatInfo FloatFormatInfo = new NumberFormatInfo()
    NegativeSign = "-",
    NumberDecimalSeparator = "."
private static NumberStyles FloatStyle = NumberStyles.AllowDecimalPoint |
    float.Parse(textInput, FloatStyle, FloatFormatInfo, out _resultFloat);

(Or TryParse, depending). That’s the full control approach.

However, if you’re working against a library you can’t really change and need a quick fix, put this in your program:

System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

Put this in your Main. Remember that it’s a hack.

Categories: Programming Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: