Why does formatting a Double using the "G" standard format string not return the full string?

1k views Asked by At

I hope I have researched this enough that my premise is not totally off base. If so, then the mathematicians out there can set me straight.

My premise is that a Double value such as 12.5 should be rounded to 5 significant figures (NOT decimal places) as 12.500. Instead, using the following C# code, I get 12.5:

Double d = 12.5;
Console.WriteLine(d.ToString("G5"));

I came across this post from 2007 which seems to echo my problem. In fact, I am using those example numbers just to keep things consistent.

My goal here is to better understand the following:

  • Is my understanding of sig figs mathematically correct? I.e., is my expectation reasonable, or is the output "12.5" somehow correct?

  • Is this really a (very long-lived) bug in the framework? If so, can/will it be fixed?

  • Assuming it is a bug, what might I do about it now? Write a hack to determine how many sig figs you actually got back and then pad it? Roll my own code to do what the "G" format string was supposed to do? I have come across examples of this on SO already, so perhaps that is evidence that a clean option does not exist.

Additionally, I do realize that the storage issues with Double might negatively impact the rounding aspect of this problem, but for now, I am only concerned with the issue of more sig figs than original digits.

EDIT: I have tested this up to framework 4.5.

2

There are 2 answers

0
Rick Davin On BEST ANSWER

See this link on G-Format Specifier. It clearly states:

The result contains a decimal point if required, and trailing zeros after the decimal point are omitted.

2
Guffa On

A Double value is rounded to 15 significant figures, not five.

Reference: The General ("G") format specifier

Rounding a number to any number of significant figures doesn't mean that the formatted string has to contain that number of digits. If the value is rounded to 12.5000000000000 then it will be formatted into "12.5" because that is the most compact way to represent the value.