commercetools / commercetools-dotnet-core-sdk

The e-commerce SDK from commercetools running on the .NET Core platform
https://docs.commercetools.com/sdk/dotnet-sdk#net-core-sdk
Apache License 2.0
10 stars 5 forks source link

AmountToDecimal() assuming 2 fraction digits #176

Closed axelivarsson closed 3 years ago

axelivarsson commented 3 years ago

TheMoney class in the dotnet core SDK uses (decimal)CentAmount / 100M; for AmountToDecimal() without considering the number of FractionDigits for the current currency.

Although most currencies has 2 fraction digits, this will give us the wrong amount for currencies like KRW which has 0 fraction digits or OMR that has 3.

I would expect this to do something like CentAmount / (decimal) Math.Pow(10, FractionDigits.GetValueOrDefault()), similar to how it is done in HighPrecisionMoney.

jenschude commented 3 years ago

The platform always returns fraction digits with a value of 2 for CentPrecisionMoney and doesn't differentiate between the currencies and their fractions so the factor would always be 100. If there is a need for different fractions you would have to use HighPrecisionMoney or create an own extension method to convert the amount to a decimal.

andersekdahl commented 3 years ago

The JSON we get from the API for a KRW price looks like this:

"value": {
  "type": "centPrecision",
  "currencyCode": "KRW",
  "centAmount": 60500,
  "fractionDigits": 0
},

The platform always returns fraction digits with a value of 2 for CentPrecisionMoney

I'm not sure what you mean, we get 0 for fractionDigits in the JSON?

jenschude commented 3 years ago

Oops. This information slipped through back then when the fractionDigits had been introduced. We will change to AmountToDecimal to use the calculation

MicheleRezk commented 3 years ago

@axelivarsson We solved it and created a new release with version 1.1.4

axelivarsson commented 3 years ago

Thank you!