1

We have web Application and user can be in different locations in europe.

Few Write number like this 5,5 and few Write like 5.5 in a textbox

We try to use that number like below

double priceOfITem = 0;
double.TryParse(((TextBox)tabTest.Rows[0].Cells[3].Controls[0]).Text.Replace(".", ","), out priceOfITem )

But this gives output as 55 which is totally wrong.

I tried like priceOfITem= Convert.ToDouble(string.Format(Thread.CurrentThread.CurrentCulture, "{0:#.##}",(((TextBox)tabTest.Rows[0].Cells[0].Controls[0]).Text)));

This Works if i type 5.5 and if i type 5,5 then it converts to 55.

Some users can type 5,5 and some can type 5.5 depending on coutry there are in. How to solve this in best manner?

James
  • 1,827
  • 5
  • 39
  • 69

1 Answers1

0

I would recommend you to parse the number with the CultureInfo-class. You need submit the user's culture e. g. from the browser-settings (visit Get CultureInfo from current visitor and setting resources based on that?).

In the C#-part, try the following snippet:

string deNumber = "5,5";
string usNumber = "6.8";
Console.WriteLine(double.Parse(deNumber, new CultureInfo("de-DE")));
Console.WriteLine(double.Parse(usNumber, new CultureInfo("en-US")));

Console output:

5.5
6.8
Seb Krae
  • 311
  • 3
  • 10