Drifting Point Number parsing: Exists a Catch All algorithm?

Among the enjoyable components of multi-cultural shows is number layouts.

  • Americans make use of 10,000.50
  • Germans make use of 10.000,50
  • French usage 10 000,50

My first strategy would certainly be to take the string, parse it in reverse till I run into a separator and also utilize this as my decimal separator. There is a noticeable imperfection with that said: 10.000 would certainly be taken 10.

An additional strategy: if the string has 2 various non-numeric personalities, make use of the last one as the decimal separator and also throw out the others. If I just have one, examine if it takes place greater than as soon as and also discards it if it does. If it just shows up as soon as, examine if it has 3 figures after it. If of course, discard it, or else, utilize it as decimal separator.

The noticeable "ideal remedy" would certainly be to identify the User's society or Browser, yet that does not function if you have a Frenchman making use of an en-US Windows/Browser.

Does the.net Framework have some mythological wizardry drifting factor parser that is far better than Double.(Try)Parse() in attempting to auto-detect the number layout?

2019-05-06 23:28:05
Source Share
Answers: 4

You can not please every person. If I enter 10 as 10.000, and also a person gets in 10 thousand as 10.000, you can not take care of that without some expertise of the society of the input. Identify the society in some way (internet browser, system setup - what is the usage instance? ASP? Inner application, or open up to the globe? ), or give an instance of the anticipated format, and also make use of one of the most forgiving parser you can. Possibly something like :

double d = Double.Parse("5,000.00", NumberStyles.Any, CultureInfo.InvariantCulture);
2019-05-07 17:52:08

I assume the most effective you can do in this instance is to take their input and afterwards show them what you assume they suggested. If they differ, show them the layout you're anticipating and also get them to enter it once more.

2019-05-07 17:44:19

The distinction in between 12.345 in French and also English is a variable of 1000. If you provide a predicted array where max < 1000 * minutes, you can conveniently presume.

Consider instance the elevation of an individual (consisting of children and also youngsters ) in mm.

By utilizing a series of 200-3000, an input of 1.800 or 1,800 can unambiguously be taken 1 meter and also 80 centimeters, whereas an input of 912.300 or 912,300 can unambiguously be taken 91 centimeters and also 2.3 millimeters.

2019-05-07 16:47:32

I do not recognize the ASP.NET side of the trouble but.NET has a rather effective class : System.Globalization.CultureInfo. You can make use of the adhering to code to parse a string having a double value :

double d = double.Parse("100.20", CultureInfo.CurrentCulture);
//  -- OR --
double d = double.Parse("100.20", CultureInfo.CurrentUICulture);

If ASP.NET in some way (i.e. making use of HTTP Request headers ) passes existing customer's CultureInfo to either CultureInfo.CurrentCulture or CultureInfo.CurrentUICulture, these will certainly function penalty.

2019-05-07 16:29:52