I have a decimal
value in my C# program and I want to convert it to the Google.Type.Money
well-known proto type. How can I do this without doing the calculations for the units and nanos manually? Is there an extension method that does this?
If you have the Google.Api.CommonProtos
NuGet package installed, the Google.Type.Money
type has a DecimalValue
property that will automatically convert back and forth with the .NET decimal
type.
create a Google.Type.Money
from a decimal
:
var money = new Google.Type.Money { DecimalValue = 123.45m }
Access the decimal
value of a Google.Type.Money
:
var decimalValue = money.DecimalValue;
Note that Google.Type.Money
is implemented with an int64
for the units and an int32
to hold the fractional portion ("nanos"), so there are some legal decimal
values in that can't be represented as a Google.Type.Money
exactly. Setting DecimalValue
will throw in these cases.