Skip to content

Commit c201984

Browse files
authored
Merge pull request #207 from commercetools/Fix-HighPrecision-Issue
2 parents 8bea399 + 4b97357 commit c201984

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

commercetools.Sdk/commercetools.Sdk.Domain/Common/HighPrecisionMoney.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,17 @@ public static HighPrecisionMoney FromDecimal(string currencyCode, decimal value,
2525
FractionDigits = fractionDigits
2626
};
2727
}
28+
public static HighPrecisionMoney FromDecimal(string currencyCode, decimal value, int fractionDigits, int centFractionDigits, MidpointRounding midpointRounding = MidpointRounding.ToEven)
29+
{
30+
var amount = Math.Round(value * (decimal) Math.Pow(10, fractionDigits), 0, midpointRounding);
31+
var centAmount = Math.Round(value * (decimal) Math.Pow(10, centFractionDigits), midpointRounding);
32+
return new HighPrecisionMoney
33+
{
34+
CurrencyCode = currencyCode,
35+
PreciseAmount = (long)amount,
36+
CentAmount = (long)centAmount,
37+
FractionDigits = fractionDigits
38+
};
39+
}
2840
}
2941
}

0 commit comments

Comments
 (0)