views:

120

answers:

2

I recently realized that some countries display floating point numbers with different comma/period notation.

ie. we would have 12,500.34, and some countries would list it as 12.500,34

This may not be a problem with display, but what about text input? For example, I imagine that if I had a decimal type and displayed it in a label, that the ToString() method (or equiv in your language of choice) would use the localization settings and display it properly, and the various parsing 'str->number' methods would probably work too.

But what about text validation? Like say I want a textbox to only allow 5 numbers, and then an optional decimal point and up to 4 more numbers? What is a common solution is this case, when you don't know what their delimiters will be? Obviously the hardcoded regex patterns i'm using now will fail, so I'm just wondering what some of you have done in these cases?

thanks for any advice

A: 

Where ever they live or however they're specified, you need to encapsulate rules that are or can be locale specific or are parsing inputs (for validation) that could be locale specific. This way you can switch the implementation out based on locale.

Depending on how and where these are you can use some frameworks (JPF does this) to apply within a framework or you can also extend your framework to work with them.

cwash
yeah, that's what I was hoping wasn't the answer, but it was my basic backup plan. I was just hoping someone had a magic bullet for this, but it seems no one does :)
LoveMeSomeCode
A: 

This is going to be very language dependent. No two languages handle this exactly alike.

I can give some advice about Java. In Java you would handle this with NumberFormat. If NumberFormat.parse() works without throwing an exception, then it's a valid number in whataver locale is being used. You can set the maximum number of decimal places that you will accept, etc. All of this is highly Java-specific, so I won't bother go into it unless you say that Java is what you're using.

Licky Lindsay