asp.net - Detect culture of number in VB.Net i.e. period or comma for decimal point / thousands separator -


in vb.net, there way of auto-detecting culture of string representation of number? i'll explain situation:

our asp.net web site receives xml data feeds boat data. of time, number format prices use either simple non-formatted integer e.g. "999000". that's easy process.

occaisionally, there commas thousands separators , periods decimal point. also, that's fine our data import understands this. example "999,000.00".

we're starting data france of prices have been entered periods , thousands separators other way around that's way it's done in many european countries. e.g. "999.000,00". our system interpret 9 hundred , ninety 9 pounds instead of 9 hundred , ninety 9 thousand pounds intended.

unfortunately, data feed contains prices in mixture of formats without culture indicator on each one. know of in-built .net functions auto-detect culture of string number based on period , comma are?

there no built-in way determine cultureinfo numeric string, far know. , doubt it'll ever be, because there no 100% safe way it.

until find better solution (eg: change on sender-side), guess best can decrease chances of error in 2 steps:

1) input data cleanup , standardization:

dim input string = " 99 9.000,00 " ' way can remove unwanted characters (anything not digit, , following symbols: ".", "-", ",") dim fixedinput string = regex.replace(input, "[^\d-,\.]", "") ' fixedinput "999.000,00" 

2) guess format:

dim indexofdot integer = fixedinput.indexof(".") dim indexofcomma integer = fixedinput.indexof(",") dim culturetestorder list(of cultureinfo) = new list(of cultureinfo) dim parsingresult double? try     if indexofdot > 0 , indexofcomma > 0         ' there both dot , comma..let's check order         if indexofdot > indexofcomma             ' dot comes after comma. should en-us culture             parsingresult = double.parse(fixedinput, numberstyles.number, cultureinfo.getcultureinfo("en-us"))         else             ' dot comes after comma. should it-it culture             parsingresult = double.parse(fixedinput, numberstyles.number, cultureinfo.getcultureinfo("it-it"))         end if     else if indexofdot = fixedinput.length-3         ' there dot! , followed 2 digits..it should en-us culture         parsingresult = double.parse(fixedinput, numberstyles.number, cultureinfo.getcultureinfo("en-us"))     else if indexofcomma = fixedinput.length-3         ' there comma! , followed 2 digits..it should en-us culture         parsingresult = double.parse(fixedinput, numberstyles.number, cultureinfo.getcultureinfo("it-it"))     end if catch end try if not parsingresult.hasvalue     try         ' there no dot or comma, or parsing failed reason. let's try less specific parsing.         parsingresult = double.parse(fixedinput, numberstyles.any, numberformatinfo.invariantinfo)     catch     end try  end if if not parsingresult.hasvalue     ' conversion not possible, throw exception or else else     ' use parsingresult.value end if 

you not 100% safe way, should still better current code (and @ least works expected on example data provided).


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -