Is .NET double.ToString thread safe?

I see a problem in an ASP.NET production application that includes the following code, which is used to render the geo-coordinates of a specific object:

private double _longitude;
private double _latitude;

public string ToCsvString()
{
    return _latitude + "," + _longitude;
}

Thread.CurrentThread.CurrentCulture will be set to different values ​​based on the incoming request. The behavior that I see is that the result of this function will depend on the threadlocal culture. From time to time, decimal points and commas are incorrect for the current crop. More strange, it seems that once erroneously, the wrong value is saved.

Does ToString have double cache values?

+3
source share
6 answers

, - . :

  • / ? , ?
  • , , ? , ? , ?
+2

ToString, ICML-, ?

+1

, , .

, 4 :

return _latitude.ToString("0.0000") + "," + _longitude.ToString("0.0000");

string.Format("{0:0.0000}, "{1:0.0000}", _latitude, _longitude);

, , ,

CultureInfo ci = System.Globalization.CultureInfo.GetCultureInfo("en-AU");
return _latitude.ToString("N4", ci) + "," + _longitude.ToString("N4", ci);
0

, Double.ToString , InvariantCulture:

someDouble.ToString(CultureInfo.InvariantCulture); 
0

, ToString , . , , , , , . ToString

0

Any page in msdn will show you that "Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members do not guarantee thread safety"

for example, look at this http://msdn.microsoft.com/en-us/library/system.delegate.aspx

Thus, double.ToString () is not guaranteed to be thread safe, as these are instance methods.

I would repeat what Anon suggested and used IformatProvider

-1
source

Source: https://habr.com/ru/post/1733448/


All Articles