Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
856 views
in Technique[技术] by (71.8m points)

.net - How to change NaN string representation in C#?

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

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

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

Example for a single thread:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...