Very strange issue with String and Double - c#

string TestVarStrg = "3.1";
double TestVarDoub = Convert.ToDouble(TestVarStrg);
MessageBox.Show(TestVarDoub.ToString());
with this code I get within the message box on the emulator "31". same on my lumia 920. but on my lumia 1520 I get "3.1". bouth devices have the last lumia black update. anyway actually I need "3.1" as double. "31" would be ok too but on all devices please. any idea about this behavior? Or an other way to convert string to double?

Please consider that the decimal sign is dependent on the culture settings. Use the overload where you can provide an IFormatProvider:
http://msdn.microsoft.com/de-de/library/9s9ak971(v=vs.110).aspx
So your code would look like:
string TestVarStrg = "3.1";
double TestVarDoub = Convert.ToDouble(TestVarStrg, System.Globalization.CultureInfo.InvariantCulture);
MessageBox.Show(TestVarDoub.ToString());
Please consider that this only works if your decimal sign is always a ".".

Related

How to parse from String to Float in C#?

This issue should be very simple but I can't find the way to make it work. I have the following code:
string yeah = "0.5";
float yeahFloat = float.Parse(yeah);
MessageBox.Show(yeahFloat.ToString());
But the MessageBox shows "5" instead of "0.5". How can I resolve this using float?
float yeahFloat = float.Parse(yeah, CultureInfo.InvariantCulture);
See documentation: http://msdn.microsoft.com/en-us/library/bh4863by.aspx
0.5 is the way some country are writing decimal number, like in America. In France, decimal number are more written with a comma : 0,5.
Typically, the code you give throw an exception on my computer.
You need to specify from what culture you are expected the string to be parse. If not, it will take your computer culture setting, which is bad, since your code could run in different countries.
So, by specifying an invariant culture, you said to the Parse function : ok, let's try to parse point or comma, try as hard as you can:
string yeah = "0.5";
float yeahFloat = float.Parse(yeah, CultureInfo.InvariantCulture);
Console.Write(yeahFloat);
There is a lot of question already on this subject :
Parse string to float number C#
Best way to parse float?
C# parsing float from string
etc..
By default, Single.Parse(String) parses based on your localization settings. If you want to use a specific one, you'll have to use an appropriate overload of that method with the culture settings that you want.
You can try this with float.TryParse() :
string yeah = "0.5";
float yeahFloat;
if (float.TryParse(yeah,System.Globalization.NumberStyles.Any,
System.Globalization.CultureInfo.InvariantCulture,out yeahFloat))
{
MessageBox.Show(yeahFloat.ToString());
}
Try passing the formatting you require in the ToString() method
MessageBox.Show(yeahFloat.ToString("0.0")); // 1 decimal place

different results on emulator and windows phone device c#

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
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

C# - Unexpected results when converting a string to double

I have a string with value "20.616378139" and when i try to convert using Convert.ToDouble or Double.Parse i get 20616378139.0 insted of the right value.
Why is this happening and how should I fix it?
You probably live in a part of the world where the decimal point is written as a comma. Fix:
var str = "20.616378139";
var dbl = double.Parse(str, System.Globalization.CultureInfo.InvariantCulture);
There's an overload to the Parse method that provides an options parameter of some kind; this is the way that you can specify for it to handle scientific notation, etc. Try setting that explicitly. If that works, then look at the default culture info settings you are using.
I've used this command and there is no problem for me before.
string s = "20.616378139";
double d = Convert.ToDouble(s);
![enter image description here][1]

Windows Phone Regional Issues

I have a text file that needs parsing in the app and this was tested on my UK device and a US device. This same text file is used by Android and iPhone apps that work fine. It has been reported to me that some people on Windows Phones this does not work!
It turns out if the device is set to a region like Germany that uses the comma "," as the decimal point then the following code does not work properly!
GeoCoordinate tempCoord = new GeoCoordinate();
tempCoord.Latitude = Convert.ToDouble(words[0]);
tempCoord.Longitude = Convert.ToDouble(words[1]);
As words comes in as a string I'm not sure how else I can get this into a double from a string?
EDIT:
On a slightly related note the following is also causing me grief!
geoWatcher.Position.Location.Latitude.ToString()
This will return 56,888 for European and 56.888 for US/UK!
Arrrgh!
Instead of using Convert.ToDouble, use double.parse(...):
double d = double.Parse("3.500,02", CultureInfo.GetCultureInfo("de-DE").NumberFormat);
There is also an overload of double.ToString() that takes a formatter, and you can use this overload to produce string representations of a double in whatever manner you wish.

Ignoring country-specific decimal separator

I'm currently doing an app, that needs to be able to work with the US number layout (123,456.78) as well as with the German layout (123.456,78).
Now my approach is to use NumberFormatInfo.InvariantInfo about like this:
temp = temp.ToString(NumberFormatInfo.InvariantInfo);
this works great when for example reading a number from a textbox. When System is set to English format it will take the . as separator, when it's set to German it will use the ,.
So far so good....but here's the problem: I have a device that returns info in the American format, and that won't change (transmitted via RS232). So I receive something like 10.543355E-00.
Now when on German setting the . will be discarded since it's just the group separator
and the number I will end up with is 10543355....which is a lot more :)
I tried with the same technique thinking this would make the whole thing kind of 'cultureless' to be able to process it independently from the system language but it didn't work :)
I hope you can maybe help me here...I'd love to use a way without having to implement the whole culture stuff etc since all I need here is really numbers that get calculated the right way.
You should use CultureInfo.InvariantCulture when parsing strings from the device. This will cause it to use the invariant culture, which has the US rules for decimal separation.
Edit in response to comments:
The issue is not when you call .ToString(), but rather when you read the string from the device, and convert it to a number:
string inputFromRS232Device = GetDeviceInput();
double value;
// You need this when converting to the double - not when calling ToString()
bool success = double.TryParse(
inputFromRS232Device,
NumberStyles.Float,
CultureInfo.InvariantCulture,
out value);

Categories

Resources