Skip to content

Instantly share code, notes, and snippets.

@RobThree
Last active October 16, 2019 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobThree/3a9c1d480e234430c514ea022df0d74f to your computer and use it in GitHub Desktop.
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/)
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
Copy link
Author

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