如何更改NaN的字符串重新presentation在C#中?字符串、如何更改、NaN、presentation

2023-09-03 04:54:09 作者:伊人℡独憔悴

我的程序节省了点云到文件,其中每个点云是一个三维点[,] ,从 System.Windows.Media.Media3D 命名空间。这说明线路输出文件(葡萄牙):

My program saves a pointcloud to file, where each pointcloud is a Point3D[,], from the System.Windows.Media.Media3D namespace. This shows a line of the output file (in portuguese):

-112,644088741971;71,796623005014;NaN (Não é um número)

而我想它是(上才能正确事后分析):

while I'd like it to be (on order to be correctly parsed afterwards):

-112,644088741971;71,796623005014;NaN

的code生成该文件的块是在这里:

The block of code that generates the file is here:

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);

这似乎是 double.NaN.ToString()方法,在 Point3D.ToString()从被称为包括了括号补充说明这是我不希望的。

It seems like the double.NaN.ToString() method, called from inside Point3D.ToString(), includes that parenthesized "additional explanation" which I don't want at all.

有没有办法来改变/覆盖此方法,让它只输出 NaN的,没有括号部分?

Is there a way to change/override this method so that it outputs only NaN, without the parentheses part?

推荐答案

Double.ToString()使用 NumberFormatInfo.CurrentInfo 格式化的数字。这最后一个属性引用到的CultureInfo 以这样的活动线程当前设置。这将默认为用户的当前语言环境。在这种情况下,它是一个葡萄牙的文化背景。为了避免这种情况,可以使用 Double.ToString(的IFormatProvider)超载。在这种情况下,你可以使用 CultureInfo.InvariantCulture

Double.ToString() uses NumberFormatInfo.CurrentInfo to format its numbers. This last property references to the CultureInfo that is currently set on the active thread. This defaults to the user's current locale. In this case its a Portuguese culture setting. To avoid this behavior, use the Double.ToString(IFormatProvider) overload. In this case you could use CultureInfo.InvariantCulture.

此外,你可以只需切换楠符号,如果你想保留的所有其他标记。通过默认全球化信息是只读的。创建克隆将解决这个问题。

Additionally you can just switch the NaN symbol if you want to retain all other markup. By default globalization information is read only. Creating a clone will get around this.

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);

要设置这在当前线程上,创建当前文化的副本,并设置对文化的数字格式信息。 pre .NET 4.5 有没有办法将其设置为所有线程。创建每个线程之后,你必须确保正确的的CultureInfo 。作为.NET 4.5有 CultureInfo.DefaultThreadCurrentCulture 定义的默认文化为的AppDomain 中的线程。当线程的文化尚未设置此设置只考虑(请参阅MSDN)。

To set this on the current thread, create a copy of the current culture and set the number format info on the culture. Pre .NET 4.5 there's no way to set it for all threads. After creating each thread you would have to ensure a correct CultureInfo. As of .NET 4.5 there's CultureInfo.DefaultThreadCurrentCulture which defines the default culture for threads within the AppDomain. This setting is only considered when the culture of the thread has not been set yet (see 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();