| 1 | import "dart:math"; |
| 2 | |
| 3 | import "package:cw_core/amount/money.dart"; |
| 4 | import "package:cw_core/amount/utils.dart"; |
| 5 | import "package:cw_core/currency.dart"; |
| 6 | |
| 7 | /// Turn a double representation of a currency amount to a proper Money representation |
| 8 | /// truncating currencies with more than 20 decimals because double can not handle more |
| 9 | extension ToMoney on double { |
| 10 | Money? tryToMoney(Currency currency) => Money.tryParse(_toSafeString(currency), currency); |
| 11 | |
| 12 | Money toMoney(Currency currency) => Money.parse(_toSafeString(currency), currency); |
| 13 | |
| 14 | String _toSafeString(Currency currency) => |
| 15 | trimTrailingFractionZeros(toStringAsFixed(min(currency.decimals, 20))); |
| 16 | } |