Domanda

I am trying to output flouting point numbers from c# in the format used by VRML 2.0.

According to the vrml spec, this is

double-precision floating point number in IEEE 1xxx floating point format (see 2.[IEEE]).

[IEEE]: ISO/IEC 9899:1990 Programming languages -- C. http://www.iso.ch/isob/switch-engine-cate.pl?searchtype=refnumber&KEYWORDS=9899 (dead link)

  • what is the correct string representation for this format? I am having trouble finding a reference. I found several explanations about the binary representation, but none about the string representation.
  • how can I configure a NumberFormatInfo or a CultureInfo in accordance with this format?

clarification:

I want to produce a vrml file and I need to know how to assure

var myCultureInfo = CultureInfo.InvariantCulture;
myCultureInfo.NumberFormat = ....
StringWriter sw = new StringWriter(MyCultureInfo)
sw.Write(myDouble);

will result in a number that is 100% in accordance to the vrml spezification.

È stato utile?

Soluzione

I saw somewhere that VRML uses the same format for floating-point numbers as ISO C. Even the VRML spec page that you're mentioning hints at that:

The SFFloat field specifies one single-precision floating point number. MFFloat specifies zero or more single-precision floating point numbers. SFFloats and MFFloats are written to the VRML file in IEEE 1xxx floating point format (see 2.[IEEE]).

Note where that hyperlink points to: to the bibliography entry for ISO C.

So you should get the proper format if you simply disable any culture-specific formatting (such as special decimal points, characters that separate blocks of three digits each, etc.). You do this by using the "invariant culture":

double number = …;
string numberEncoding = number.ToString(CultureInfo.InvariantCulture.NumberFormat);

See also Standard Numeric Format Strings if you want to control the output format of the number, e.g.

  • for the scientific format:

    number.ToString("E", CultureInfo.InvariantCulture.NumberFormat)
    
  • for the round-trip format:

    number.ToString("R", CultureInfo.InvariantCulture.NumberFormat)
    

…and so on.

P.S.: If you don't quite trust that the invariant culture will be exactly the right match, you could always create your own instance of NumberFormatInfo:

var vrmlNumberFormat = new NumberFormatInfo { NumberDecimalSeparator = ".", … };
string numberEncoding = number.ToString(vrmlNumberFormat);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top