docs
docs copied to clipboard
The proposed solution for encoding decimals does not work with decimal.MaxValue
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.
- ID: c2a41d02-3084-c31e-98ed-2bd786c7a301
- Version Independent ID: 31a20696-f326-63c3-77d1-f3cfc0aa589c
- Content: Protobuf scalar data types - gRPC for WCF developers
- Content Source: docs/architecture/grpc-for-wcf-developers/protobuf-data-types.md
- Product: dotnet-architecture
- Technology: grpc
- GitHub Login: @JamesNK
- Microsoft Alias: jamesnk
Hi @JamesNK, any suggestion regarding the above issue?
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 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.
Sure, that's a good improvement.
Fixed by #26525