0

Take a look at the following C# code:

decimal value1 = 1234M;
decimal value2 = 1234.00M;
Console.WriteLine(value1 == value2); // True
Console.WriteLine(value1);           // 1234
Console.WriteLine(value2);           // 1234.00

What are the differences between value1 and value2 that cause them to be formatted differently (different number of decimal places)?

They are equal values, so I'd expect that they'd be formatted in the same way. Does value2 somehow store somewhere that I created it with two decimal places? If yes, how can I see that and change that?

Tom Pažourek
  • 9,582
  • 8
  • 66
  • 107
  • 2
    Decimal is a bit special, it is capable of keeping track of the number of significant digits in the value. Very unlike float and double. So if you don't specify how many digits you want in the fraction then it will display as many as it knows are accurate. – Hans Passant Oct 17 '17 at 16:42
  • "I'd expect that they'd be formatted in the same way" Which way would you expect? Would you expect it to add significant digits to the first value or remove them from the second? – D Stanley Oct 17 '17 at 17:18
  • 1
    @DStanley If two values are equal, I expect that they should behave the same. I don't really care how they are formatted as long as they are consistent. This is just unexpected; it feels like I have no way of knowing how a decimal value will be formatted until I actually do it. If it were up to me, I would make the default formatting work the same way as the floating point types - removing all non-significant zeros. – Tom Pažourek Oct 18 '17 at 07:35

2 Answers2

1

Internally the decimal is stored as a type of floating point number, so it stores the mantissa and the exponent. In your case the two numbers are stored differently, something roughly like:

decimal value1 = 1234M;
// mantissa = 1234, exponent = 0

decimal value2 = 1234.00M;    
// mantissa = 123400, exponent = 2

When output, it looks like .NET gives you get the 'whole' mantissa with a decimal point positioned according to the exponent.

codeulike
  • 22,514
  • 29
  • 120
  • 167
  • Is it possible to get the mantissa value and the exponent value somehow? My issue is that those values are two different things that each behave differently, and I should be able to detect that they are not identical. If `value1` behaves some way and `value2` behaves some other way, they are not equal values to me. – Tom Pažourek Oct 18 '17 at 07:38
  • Mathematically, 1234 and 1234.00 are equal. The datatype is an abstraction representing a number, and the numbers are equal. If you are concerned with establishing whether the underlying representation of the value in memory is the same, you are on a different path to most people using the decimal datatype. So I don't think there's a simple operator that can test for that. You can however delve into the internal representation of the value - see this answer here https://stackoverflow.com/a/3801462/22194 – codeulike Oct 18 '17 at 09:26
  • Or if you use a `float` I think it is normalised, which means the internals are managed so that 1234 and 1234.00 would be stored in the same way. – codeulike Oct 18 '17 at 09:31
1

The way you did has added different precision in your decimal value.

It's the same value but not the same format

A similar subject has been reported here : Adjusting decimal precision, .net

Anthony Giretti
  • 327
  • 1
  • 2
  • 8