Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid Double.ToString() result in razor code generating javascript

I use the following razor code to generate some javascript to produce markers on a Google map.

@foreach(Point point in Model.Points.Take(3))
{
    String longitude = point.Longitude.ToString(CultureInfo.InvariantCulture);
    String latitude = point.Latitude.ToString(CultureInfo.InvariantCulture);
    <text>
        var location = new google.maps.LatLng(@(longitude), @(latitude));
        bounds.extend(location);
        var marker = new google.maps.Marker({
            position: location,
            map: map
        });
    </text>
}

In development, this correctly becomes:

var location = new google.maps.LatLng(52.2124273, 5.9545532);
bounds.extend(location);
var marker = new google.maps.Marker({
    position: location,
    map: map
});

However, on our production server, it becomes:

var location = new google.maps.LatLng(522124273000, 59545532000);
bounds.extend(location);
var marker = new google.maps.Marker({
    position: location,
    map: map
});

Which is producing just a grey Google map. What is causing this strange ToString behavior?

edit

The Point class is a custom class, not from a library. Here are the relevant parts:

public class Point
{
    private Double latitude;
    private Double longitude;

    public Double Latitude
    {
        get
        {
            return latitude;
        }
    }

    public Double Longitude
    {
        get
        {
            return longitude;
        }
    }
}
like image 413
Johan van der Slikke Avatar asked May 15 '14 08:05

Johan van der Slikke


1 Answers

JK and AlexC were right, see their comments on my question. The data was coming in wrong from the external API. Setting the global culture to en-US solved the issue.

I only do not understand one thing: I'm using a library which uses JSON.net, everywhere in the documentation it says the parsing is done with InvariantCulture. So I would think the global culture would not make a difference.

like image 82
Johan van der Slikke Avatar answered Nov 15 '22 14:11

Johan van der Slikke