Format double output with dot instead of comma

Asked

Viewed 2,308 times

4

I have a collection of coordinates to popular the Peater, with city name, latitude and longitude.

 .aspx

 <form id="form1" runat="server">

    <div id="repetidor">
        var markers = [
                <asp:Repeater ID="rptMarkers" runat="server" Visible="true">
                <ItemTemplate>
                  cidade: <%# Eval("NomeCidade")%>,
                     lat: <%# String.Format(System.Globalization.CultureInfo.GetCultureInfo("en-US"), "{0:D}", Eval("Latitute")) %>,
                     lng: <%# String.Format(System.Globalization.CultureInfo.GetCultureInfo("en-US"), "{0:D}", Eval("Longitude")) %>,
                </ItemTemplate>
            <SeparatorTemplate>
                    -
            </SeparatorTemplate>
                </asp:Repeater>
        ]
    </div> 

I’ll get the coordinates like this;

     .aspx.cs            

       (...)
                var address = coordenadas.NomeCidade + ", Brasil";
                var locationService = new GoogleLocationService();
                var point = locationService.GetLatLongFromAddress(address);
                coordenadas.Latitude = point.Latitude;
                coordenadas.Longitude = point.Longitude;
                coordenadasColecao.Add(coordenadas);

        (...)
            rptMarkers.DataSource = coordenadasColecao;
            rptMarkers.DataBind();
        (...)

I’d like it to come out like this: 46.1475292 instead of 46.1475292

  • Is latitude a variable? Why are you using Eval? Could you give an example that we can reproduce?

  • yes, a variable. I populate a Repeater with a collection of coordinates.

  • And the example, where? I asked 3 questions, you only answered 1 and 2, put a clearer example of the code so that we can reproduce to understand your need and can give suggestions that really work how to solve.

  • yes, I posted the code I’m using.

  • What value is passing in this part Eval("Latitute")?

  • the latitude with the comma.

Show 1 more comment

3 answers

3

I don’t know how you’re getting this amount, because usually it comes with dot, as can be seen on the official Github of the API you are using.

But you can make a simple Replace(), if you wish.

Console.WriteLine(String.Format(System.Globalization.CultureInfo.GetCultureInfo("en-US"), "{0:D}", "46,1475292").Replace(",","."));

You also have the strings to format the way you want.

Otherwise, you can look at the question of How to Format Double I think you’ll have a few more ideas on how to do what you want.

  • could not with replace.

  • @Daniel Made a mistake?

  • Format specifier was invalid.

2


2

Utilize String.Format and also force with the Globalization

<%# string.Format(new System.Globalization.CultureInfo("en-US"), 
                              "{0:D}", Eval("Latitude")) %>
  • Exception Details: System.Formatexception: Format specifier was invalid.

  • I guess he wasn’t coming as decilmal then

  • the control displays with comma, but the variable contains the point. It was solved with the solution proposed by @Virgilio Novic

  • 1

    I did the answer before editing the question, as it is a string and will continue string a simple replace works, if you need decimal then use my answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.