locked
Math operations: different results from iOS emulator to real device.Maybe culture problems? RRS feed

  • Question

  • User299957 posted

    Hi, I create an app with some calculation on distance between geoloc points. On my emulator all works ( Region and Language are: Language English and Region United States ), but on my device ( region and language are italian ) the results are wrong. I think a problem about of culture because I see in the app the number with the dot as comma that it the number are in US or EN culture. How to manipulate the numbers independently by culture?

    Monday, September 18, 2017 10:28 AM

All replies

  • User89714 posted

    @biapar - All of the usual conversion methods have locale-specific and locale-independent options.

    Converting a double to a string in a locale independent way is done by:

    value.ToString(CultureInfo.InvariantCulture)
    

    See https://msdn.microsoft.com/en-us/library/shxtf045(v=vs.110).aspx

    Monday, September 18, 2017 10:37 AM
  • User299957 posted

    I find the problem, but not the solution. This code return the correct results, if I setup my phone with English language and United States region. See images.

    Monday, September 18, 2017 12:02 PM
  • User89714 posted

    @biapar - Show the code where you take the double value and convert it to a string for display (which could be in a value converter or elsewhere).

    Also. what result do you want? Do you want it to always use a "." for the decimal separator, or do you want it to use the appropriate character for the current locale?

    Monday, September 18, 2017 3:19 PM
  • User171749 posted

    You can force a Culture on the app which would fix the problem Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");

    This would go in the platform specific code.

    Monday, September 18, 2017 3:22 PM
  • User299957 posted

    Hi, thank for your reply. I wish to return the correct result by current locale. That is 1.2345,45 for ITA.

    Monday, September 18, 2017 3:24 PM
  • User89714 posted

    @biapar - Show the code where you take the double value and convert it to a string for display (which could be in a value converter or elsewhere).

    Monday, September 18, 2017 3:43 PM
  • User299957 posted

    Distance calculation:

    private double distance(double lat1, double lon1, double lat2, double lon2, char unit) { double theta = lon1 - lon2; double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta)); dist = Math.Acos(dist); dist = rad2deg(dist); dist = dist * 60 * 1.1515; if (unit == 'K') { dist = dist * 1.609344; } else if (unit == 'N') { dist = dist * 0.8684; } return (dist); }

        private double deg2rad(double deg)
        {
            return (deg * Math.PI / 180.0);
        }
    
        private double rad2deg(double rad)
        {
            return (rad / Math.PI * 180.0);
        }
    

    Variable Assignment ( elemento.Distanza is a Double ):

    elemento.Distanza = Math.Round(distance(lastmypoint.Latitude, lastmypoint.Longitude, pointB.Latitude, pointB.Longitude, 'K'),2);

    Tuesday, September 19, 2017 5:59 AM
  • User89714 posted

    @biapar - Show the code where you take the double value and convert it to a string for display (which could be in a value converter or elsewhere).

    The calculation code only operates on doubles. It's the code that takes the doubles and goes on to display them that is relevant here.

    Tuesday, September 19, 2017 9:39 AM
  • User299957 posted

    @JohnHardman said: @biapar - Show the code where you take the double value and convert it to a string for display (which could be in a value converter or elsewhere).

    The calculation code only operates on doubles. It's the code that takes the doubles and goes on to display them that is relevant here.

    I assign the value with this code: elemento.Distanza = Math.Round(distance(lastmypoint.Latitude, lastmypoint.Longitude, pointB.Latitude, pointB.Longitude, 'K'),2);

    and show the value with Label

    Wednesday, September 27, 2017 7:40 PM
  • User299957 posted

    I solved the problem. The problem was caused by dot into the item value and in Italian culture was used like thousand separator.

    Position pointB = new Position(double.Parse(item.Lat, CultureInfo.InvariantCulture), double.Parse(item.Long, CultureInfo.InvariantCulture));
    
    Thursday, October 12, 2017 7:03 PM