Question

I am using .NET 3.5 framework with C# My application going to use worldwide. (Windows 7, 64 bit)

I am having one double/float number which later on convert into string. But on different OS it's changing the value like 46.57 become 46,57

How can I stop this ? Is .NET can handle this internally ?

Below is my code;

Decimal lon = Convert.ToDecimal(dt.Rows[j]["lon"]);    
Decimal lat = Convert.ToDecimal(dt.Rows[j]["lat"]);    
lon = lon / 100000;    
lat = lat / 100000;    
string longitude = Convert.ToString(lon);    
string latitude = Convert.ToString(lat);
Was it helpful?

Solution 2

String representation of a decimal value is culture specific.

Convert.ToString(decimal) method uses current culture thread.

Here how it's implemented;

public static string ToString(decimal value)
{
     return value.ToString(CultureInfo.CurrentCulture);
}

Probably one of your OS uses a culture that have . as NumberDecimalSeparator and the other one uses a culture that have , as a NumberDecimalSeparator.

How can I stop this ? is .NET can handle this internally ?

.NET can't handle this. It uses whatever it founds as a current culture.

But you can.

Using the same culture thread in your both OS can solve this issue or you can use CultureInfo.Clone method to clone your current culture and set your NumberDecimalSeparator property what ever string you want.

OTHER TIPS

As explained by others, this is default and desired behavior.

You can use the InvariantCulture to always output in the same format:

string longitude = Convert.ToString
                   ( lon
                   , System.Globalization.CultureInfo.InvariantCulture
                   );

The differences are a function of the culture on each machine/OS. I would encourage you to be more explicit with the ToString() conversion. Without those parameters you are leaving it to .NET to make the decision for you. For example:

string longitude = lon.ToString("G", new CultureInfo("en-US"));

You can find more examples of valid conversions here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top