hel*_*ker 8 .net c# tostring nan
我的程序将一个pointcloud保存到文件中,其中每个pointcloud都是Point3D[,]来自System.Windows.Media.Media3D命名空间的.这显示了输出文件的一行(用葡萄牙语):
-112,644088741971;71,796623005014;NaN (Não é um número)
Run Code Online (Sandbox Code Playgroud)
虽然我希望它(为了以后正确解析):
-112,644088741971;71,796623005014;NaN
Run Code Online (Sandbox Code Playgroud)
生成文件的代码块在这里:
var lines = new List<string>();
for (int rows = 0; rows < malha.GetLength(0); rows++) {
for (int cols = 0; cols < malha.GetLength(1); cols++) {
double x = coordenadas_x[cols];
double y = coordenadas_y[rows];
double z;
if ( SomeTest() ) {
z = alglib.rbfcalc2(model, x, y);
} else {
z = double.NaN;
}
var p = new Point3D(x, y, z);
lines.Add(p.ToString());
malha[rows, cols] = p;
}
}
File.WriteAllLines("../../../../dummydata/malha.txt", lines);
Run Code Online (Sandbox Code Playgroud)
似乎double.NaN.ToString()从内部调用的方法Point3D.ToString()包括括号内的"额外解释",我根本不需要.
有没有办法更改/覆盖此方法,以便它只输出NaN,没有括号部分?
Car*_*iel 11
Double.ToString()用于NumberFormatInfo.CurrentInfo格式化其数字.最后一个属性引用CultureInfo当前在活动线程上设置的属性.这默认为用户的当前区域设置.在这种情况下,它是葡萄牙文化背景.要避免此行为,请使用Double.ToString(IFormatProvider)重载.在这种情况下,您可以使用CultureInfo.InvariantCulture.
此外,如果要保留所有其他标记,只需切换NaN符号即可.默认情况下,全球化信息是只读的.创建克隆将解决这个问题.
System.Globalization.NumberFormatInfo numberFormatInfo =
(System.Globalization.NumberFormatInfo) System.Globalization.NumberFormatInfo.CurrentInfo.Clone();
numberFormatInfo.NaNSymbol = "NaN";
double num = double.NaN;
string numString = System.Number.FormatDouble(num, null, numberFormatInfo);
Run Code Online (Sandbox Code Playgroud)
要在当前线程上设置此项,请创建当前区域性的副本,并在区域性上设置数字格式信息.在.NET 4.5之前,没有办法为所有线程设置它.创建每个线程后,您必须确保正确CultureInfo.从.NET 4.5开始,CultureInfo.DefaultThreadCurrentCulture它定义了内部线程的默认文化AppDomain.仅在尚未设置线程的区域性时才考虑此设置(请参阅MSDN).
单个线程的示例:
System.Globalization.CultureInfo myCulture =
(System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
myCulture.NumberFormat.NaNSymbol = "NaN";
System.Threading.Thread.CurrentThread.CurrentCulture = myCulture;
string numString = double.NaN.ToString();
Run Code Online (Sandbox Code Playgroud)