Last active
October 16, 2019 23:17
-
-
Save RobThree/3a9c1d480e234430c514ea022df0d74f to your computer and use it in GitHub Desktop.
Provide c# decimal datatype for gRPC (based on https://visualrecode.com/blog/csharp-decimals-in-grpc/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
namespace YOUR_NAMESPACE_HERE | |
{ | |
public partial class DecimalValue | |
{ | |
public DecimalValue(int[] bits) => Bits.AddRange(bits); | |
public static implicit operator decimal(DecimalValue decimalValue) => decimalValue.ToDecimal(); | |
public static implicit operator DecimalValue(decimal value) => FromDecimal(value); | |
public decimal ToDecimal() => new decimal(Bits.ToArray()); | |
public static DecimalValue FromDecimal(decimal value) => new DecimalValue(decimal.GetBits(value)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Name "DecimalValue" prevents conflict with C# Decimal type | |
message DecimalValue { | |
repeated int32 bits = 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://twitter.com/RobIII/status/1181903337316376576 for good reason NOT to use this.