Convert "-12.0000"(string) to -12.0000(double). Is it possible?

Asked

Viewed 47 times

2

It is possible to convert "-12.0000"(string) for -12.0000(double) ?

Follows code:

var valor = "-12.0000";
var result = Convert.ToDouble(valor, CultureInfo.InvariantCulture); //-12 <--- perde zeros

Or if you prefer . NET Fiddle: https://dotnetfiddle.net/RnFB1f

  • I imagine you will use it differently, after all in this code the conversion is not necessary except for demonstration. Depending on how you use it needs to be quite different from this.

  • @Maniero It is possible to convert "-12.0000"(string) for -12.0000(double) without losing the "0000". ?

  • 2

    @Matheusmiranda Double is a type of data that stores values, not formatting for display, ie, 12.00000000000000 is the same thing as 12 only, zeros are redundant, they alone are not a value and are not considered. This you want can be achieved with Tostring("0.0000"). Example: result.ToString("0.0000") where the result variable is double.

  • 1

    @Murarialex is what Murarialex said. Number follows mathematical rules, text is just drawing of the numbers you mount as you see fit.

1 answer

0

You will have to pass the culture not to lose the 0, will be like this:

double longitude = double.Parse(text, CultureInfo.InvariantCulture);

It is advisable to always use double for latitude and longitude.

Browser other questions tagged

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