0

Consider the following code:

// Float
Console.WriteLine(123.0f);    // 123

// Double
Console.WriteLine(123.0);     // 123

// Decimal
Console.WriteLine(123.0m);    // 123.0

Questions

  • Why does decimal maintain a single digit precision, but float and double do not?
  • Is there a way to preserve this information for float and double, even if when zero?
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
  • 1
    And no, there isn't a way to preserve this information in a floating point number. – GSerg Jul 29 '22 at 16:14
  • 1
    @GSerg: Yes, there is. In IEEE 754-2019 decimal formats, there may be multiple representations of a given number. The set of such representations is called a cohort. The standard defines preferred exponents for the results of operations that best reflect the quanta of the operands, so that adding “3.0” to “4.00” will produce “7.00” and multiplying them will produce “12.000”. The standard also specifies operations for manipulating the quanta. – Eric Postpischil Jul 29 '22 at 21:51

0 Answers0