逗号插入无穷大的小数中

Commas are inserted in infinite decimals

本文关键字:小数 无穷大 插入      更新时间:2023-09-26

我正在VS中开发一个应用程序,该应用程序使用谷歌地图多段线的全局坐标。我使用的是C#3.5,在Windows8上运行。运行时,我的程序会生成一些坐标,并将它们输出到html文件中的js代码中。在我的电脑上,这个程序运行良好,它正在完成自己的工作,并生成一个坐标如下的js代码:

new google.maps.LatLng(47.6370775, 19.0505071666666667),
new google.maps.LatLng(47.637077333333333, 19.0505070),
new google.maps.LatLng(47.6370775, 19.0505066666666667),

然而,当我在以后要使用的计算机上运行程序时,发生了一些奇怪的事情,它输出了这样的代码:

new google.maps.LatLng(47.6370775, 19.0505071,666666667),
new google.maps.LatLng(47.637077,333333333, 19.0505070),
new google.maps.LatLng(47.6370775, 19.0505066,666666667),

注意无限小数前的逗号。这台电脑运行的是Windows XP SP3,所以它是一个旧得多的系统,但上面安装了.NET 3.5。使用这些坐标会在谷歌地图中产生意想不到的结果(它将中心点放在叙利亚,而不是匈牙利)。我不确定操作系统或我的应用程序是否进行了更改,我无法调试,因为这个系统没有安装VS(我无法安装它)。

更新:

从文本文件获取的原始坐标:

01903.43995

转换方法(纬度是上面字符串中的坐标)

latitude = latitude.TrimStart('0');
latitude = latitude.Insert(2, ".");
string[] currentcoord = latitude.Split('.');
string temp = currentcoord[1] + currentcoord[2];
string[] temp2;
if ((((double.Parse(temp) / 60) * 100).ToString()).Contains("."))
{
   temp2 = (((double.Parse(temp) / 60) * 100).ToString()).Split('.');
   currentcoord[1] = temp2[0] + temp2[1];
}
else
{
   currentcoord[1] = ((double.Parse(temp) / 60) * 100).ToString();
}
converted += currentcoord[0] + "." + currentcoord[1] + ";";

"converted"是转换的结果,即用作输出的坐标。(在这种情况下,它应该是19.0505071666666667或类似的值)。

另一台计算机可能具有不同的区域设置,因此.ToString()生成的值不包含您所依赖的'.'

您应该使用.ToString(CultureInfo.InvariantCulture)Parse(s, CultureInfo.InvariantCulture)