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
Related
I try to use the geolocation function from Windows Phone 8.1, and it works when the emulated Windows Phone has its regional setting on "English (United State)", but my application is targetting mostly French people, and when I try with the regional setting (by default on my computer) "French (France)" the longitude and lattitude values are irrelevant, because they are rounded.
My code:
Geolocator watcher = new Geolocator();
watcher.DesiredAccuracy = PositionAccuracy.High;
Geoposition position = await watcher.GetGeopositionAsync();
double lat = position.Coordinate.Point.Position.Latitude;
double lon = position.Coordinate.Point.Position.Longitude;
With regional setting on "English (United State)", lat and lon get values like "48.12345", when I keep default / "French (France)" regional setting, they get "48.0" (and the values are already incorrects into the watcher object).
I actually tried many thing (I suspect the decimal separator to be guilty maybe the geolocation simulator sent value like "48,12345", but the parsing of these value is out of my scope, into sealed functions).
So I tried to change regional format in apps many ways, but nothing works for now.
Someone know a workaround about this? (I found some similar case on the internet with German developers, but I don't really find a solution...)
Since you pointed out that this problem happens with your emulator and not on an actual phone you should check your system settings.
I also used to have numerous issues with the WP geolocation feature on my dev machines and am now using the "English" standardization of decimal grouping to prevent these problems.
There is an article that explains how to do the setup (which is fairly easy but the downside is that it also affects the display of all numbers on your system such as in Office..):
http://dotnetbyexample.blogspot.de/2014/05/windows-phone-emulatorunable-to-create.html
I'm having a problem with a Windows Phone 7 application that I'm coding (Using Visual Studio 2010). My code looks like this:
private void Key1Input_TextChanged(object sender, EventArgs e)
{
string hexOnly= Key1Input.Text;
int n = 0;
if (!int.TryParse(hexOnly, System.Globalization.NumberStyles.HexNumber, System.Globalization.NumberFormatInfo.CurrentInfo, out n) &&
hexOnly!= String.Empty)
{
Key1Input.Text = hexOnly.Remove(hexOnly.Length - 1, 1);
Key1Input.SelectionStart = Key1Input.Text.Length;
}
}
which is a same code that can also be found from this web site: http://social.msdn.microsoft.com/Forums/windows/en-US/ec7b777d-deb1-45e1-b66e-e25daddf6497/text-box-which-accepts-only-hexadecimal-values
The code works fine, but when I tried to insert 9th character to the textbox, it didn't show up. My textbox's Maxlength value is 12. After inserting some breakpoints to the code and debugging the app, I noticed it goes inside the if statement when there's 8 hex numbers and trying to type more. Am I doing something wrong, or are there some kinds of limitations with WP7.
I also tried the other 2 codes from the same site. The second one works, while the third one does not.
The smallest 8-digit hex number is F0000000, which is greater than Int32.MaxValue. Try long instead of int.
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 ".".
We wrote a program that behaves differently on two equal machines.
On one machine is shows the price with a pound symbol. We set the symbol based upon a list e.g.
{ "£", "€", "€" };
Both machines are configured with the same language and keyboard settings.
We don't depend on currency settings, but even so the currency setting on both machines are the same.
But on one machine, the symbol is "translated" to the letter J.
We have this with a label on a windows form, using C# 2010 and wonder what could cause it?
update
I use the same font and same method to display it.
Both machines run Win7 sp1
And its not only with the pound symbol also the Euro symbol is shown as a J ???
I've come up with something thanks to my awesome Google-fu, but I could only test it on my one mahcine, so I'm not sure if it'll do it for you, but give it a shot anyway.
var encoder = System.Text.Encoding.GetEncoding(858); //also tried with 437
byte[] c = new byte[] { 156 };
Console.WriteLine(encoder.GetString(c));
This prints out "£" on my machine.
Taken from this thread: http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/dccd38e0-6f9e-4fb2-b114-bc44128ca634/
We had a similar issue with websites (our own and one from other well established websites) where the pound (£) symbol showed as a letter J. We also noticed that at the same time the text was bold and oblique when it shouldn't have been.
After much investigation it turned out that much of the machines we came across had something wrong with the Helvetica font installed on the machines, and there are two possible solutions:
If you have control over the machines delete the "Helvetica Bold Oblique", "Helvetica Bold" and "Helvetica Oblique" fonts ("Helvetica Regular" seems to work fine).
Use a different font in your applications/websites.
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.