Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

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

biaparbiapar ITMember ✭✭✭
edited September 2017 in Xamarin.Forms

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?

Posts

  • JohnHardmanJohnHardman GBUniversity admin

    @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

  • biaparbiapar ITMember ✭✭✭

    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.


  • JohnHardmanJohnHardman GBUniversity admin

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

  • seanydaseanyda GBMember ✭✭✭✭✭
    edited September 2017

    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.

  • biaparbiapar ITMember ✭✭✭

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

  • JohnHardmanJohnHardman GBUniversity admin

    @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).

  • biaparbiapar ITMember ✭✭✭

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

  • JohnHardmanJohnHardman GBUniversity admin

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

  • biaparbiapar ITMember ✭✭✭

    @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

  • biaparbiapar ITMember ✭✭✭

    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));
    
Sign In or Register to comment.