docs icon indicating copy to clipboard operation
docs copied to clipboard

The proposed solution for encoding decimals does not work with decimal.MaxValue

Open pjh1974 opened this issue 1 year ago • 4 comments

The proposed solution for handling decimal values (Creating a custom decimal type for Protobuf) does not work for large decimal values. If you use decimal.MaxValue as an example, this will fail with a System.OverflowException on the following line of code:

var units = decimal.ToInt64(value);


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

pjh1974 avatar Sep 13 '22 10:09 pjh1974

Hi @JamesNK, any suggestion regarding the above issue?

erjain avatar Sep 16 '22 09:09 erjain

That's by design for the supplied code.

If you want to exactly store all decimal values then the best method is to convert it to a string in protobuf and parse it back again on the other side.

JamesNK avatar Sep 16 '22 10:09 JamesNK

@JamesNK I'm OK with that but I do feel the tone of the documentation suggests that the example provided is better than using strings, I quote: "There are multiple other algorithms for encoding decimal values as byte strings, but this message is easier to understand than any of them".

I would at minimum, point-out the limitations of the suggested approach, as it is likely that most people would assume that this example works for all decimal values; which it doesn't. Perhaps something along the lines of:

Note: This example will only support the encoding of values with units between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807, and only to a precision 9 decimal places. The decimal data type in .NET supports a much wider range of values and to a precision of 28 decimal places. If you need to support all possible values, an alternative approach will be required.

pjh1974 avatar Sep 16 '22 11:09 pjh1974

Sure, that's a good improvement.

JamesNK avatar Sep 16 '22 12:09 JamesNK

Fixed by #26525

IEvangelist avatar Oct 27 '22 12:10 IEvangelist