1

So i am working on an application that does some calculations. It reads some numbers from a txt, it converts them to double and after it multiplies them it gives the result.

Lets say the txt has the numbers 10.5 and 2

string string1 = "10.5", string2 = "2";
double double1 = Convert.ToDouble(string1), double2=Convert.ToDouble(string2);
double double3=double1*double2;
textbox.text= double3.ToString();

The result I always get on emulator is 21 while on my device i get 210. I tried reinstalling the app from the phone, restarting the phone and the pc and i tried this over 10 times. I still get different results on my phone. What should i do?

PS: i tried double.parse but still the same

user1924391
  • 255
  • 5
  • 13
  • 1
    Sounds like a culture problem? Could it be reading the `.` as a thousands separator and ignoring it? – lc. Jan 19 '13 at 14:57
  • so you are suggesting to add "," instead of "." ? – user1924391 Jan 19 '13 at 14:59
  • but on emulator it works just fine. why is this? – user1924391 Jan 19 '13 at 15:00
  • @user1924391 The phone and the emulator may be working under different locales. One interprets '.' as a decimal point, the other interprets its as a number separator. – Peter M Jan 19 '13 at 15:02
  • Because the emulator is running the same locale as your computer; the phone may not. Try a `Double.Parse` and specify the culture and see if that fixes it. – lc. Jan 19 '13 at 15:05

1 Answers1

1

On the basis that the phone and emulator are working under different locales, then this SO question answers what is really being asked how-to-convert-string-to-double-with-proper-cultureinfo

Of course you are now going to have to match your text file to the corrected locale.

Also see what-does-cultureinfo-invariantculture-mean

Community
  • 1
  • 1
Peter M
  • 7,309
  • 3
  • 50
  • 91
  • thanks. i will check it out. the txt uses "." as a decimal point and i want the phone no matter what locale its working under to use it as a decimal point too. – user1924391 Jan 19 '13 at 15:07
  • i tried the following and it worked on both phone and emulator. thank you all guys! double.Parse(Settings.appSettings[tcholcLog].ToString(), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo); – user1924391 Jan 19 '13 at 15:10