[Mono-list] Argh... ECMA & MS (was: Number style / format stuff)
Derek Holden
dsh2120@draper.com
Thu, 20 Sep 2001 11:21:25 -0400
Making good progress on number formatting. I can use the mcs system
libraries and get many number formats out, including default types called
when just doing a Console.WriteLine ("{0}", n).
I've been using the ECMA docs as the basis, and as always there's a heap of
inconsistencies.
For instance, take the easiest case:
int f = 5;
string s = f.ToString ();
MS yeilds "5" as one would expect.
ECMA docs will give you "5.00" or "5.0000000000" depending on how you feel
ToString w/ no args is defined as using format "G" (general) w/ default
number formatting (NumberFormatInfo.CurrentInfo).
Let's take a look at the definiton for ToString w/ general format
----------------------------
Strings are formatted in either Fixed Point or Exponential
format. Results are rounded when needed. If no percision is
given, the defaults are:
short & ushort: 5
int & uint: 10
long & ulong: 19
float: 7
double: 15
decimal: 29
The value is formatted using fixed-point if exponent >= -4
and exponent < percision, where exponent is he exponenent of
the value in exponential format. The decimal point and trailing
zeros are removed when possible.
For all other values, exponential format is used. The case of
the format specifier determines whether 'e' or 'E' prefixes
the exponent.
In either case, the number of digits that appear in the result
(not including the exponent) will not exceed the value of the
percision. The result is rounded as needed.
Integral values are formatted using Fixed Point whenever
percision is omitted.
---------------------------------------------
First of all, that last line conflicts with the first paragraph. If
percision is not specified, do you just do a fixed point format, or set it
to 10 and proceed? 10 and fixed point? Fixed point formatting states:
------------------------------------------------------
Used for strings in the following form "[-]M.DD...D"
At least one non-zero digit precedes the '.', w/ a
'-' before that if negative. Percision specifies number
of decimal places 'D' to go. If not given, use
NumberFormatInfo.NumbeDecimalDigits. Results are rounded
if necessary.
------------------------------------------------------
If percision is omitted, we use the current culture invariant
NumberDecimalDigits (2 for us).
So we're looking at 5.00.
It's not too difficult to work in proper behavior, it's just frustrating.
These ECMA docs are way easier to use as a design document than the .NET
Framework SDK heap. Okay, end rant.