Skip to content

Instantly share code, notes, and snippets.

@RobThree

RobThree/DecimalValue.cs

Last active Oct 16, 2019
Embed
What would you like to do?
Provide c# decimal datatype for gRPC (based on https://visualrecode.com/blog/csharp-decimals-in-grpc/)
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));
}
}
// Name "DecimalValue" prevents conflict with C# Decimal type
message DecimalValue {
repeated int32 bits = 1;
}
@RobThree

This comment has been minimized.

Copy link
Owner Author

@RobThree RobThree commented Oct 16, 2019

See https://twitter.com/RobIII/status/1181903337316376576 for good reason NOT to use this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment