| 1 | import 'dart:math'; |
| 2 | |
| 3 | import 'package:cw_core/utils/print_verbose.dart'; |
| 4 | import 'package:decimal/decimal.dart'; |
| 5 | import 'package:decimal/intl.dart'; |
| 6 | import 'package:fluttertoast/fluttertoast.dart'; |
| 7 | import 'package:intl/intl.dart'; |
| 8 | |
| 9 | class ZanoFormatter { |
| 10 | static const defaultDecimalPoint = 12; |
| 11 | |
| 12 | //static final numberFormat = NumberFormat() |
| 13 | // ..maximumFractionDigits = defaultDecimalPoint |
| 14 | // ..minimumFractionDigits = 1; |
| 15 | |
| 16 | static Decimal _bigIntDivision({required BigInt amount, required BigInt divider}) { |
| 17 | return (Decimal.fromBigInt(amount) / Decimal.fromBigInt(divider)).toDecimal(); |
| 18 | } |
| 19 | |
| 20 | static String intAmountToString(int amount, [int decimalPoint = defaultDecimalPoint]) { |
| 21 | final numberFormat = NumberFormat() |
| 22 | ..maximumFractionDigits = decimalPoint |
| 23 | ..minimumFractionDigits = 1; |
| 24 | return numberFormat |
| 25 | .format( |
| 26 | DecimalIntl( |
| 27 | _bigIntDivision( |
| 28 | amount: BigInt.from(amount), |
| 29 | divider: BigInt.from(pow(10, decimalPoint)), |
| 30 | ), |
| 31 | ), |
| 32 | ) |
| 33 | .replaceAll(',', ''); |
| 34 | } |
| 35 | |
| 36 | static String bigIntAmountToString(BigInt amount, [int decimalPoint = defaultDecimalPoint]) { |
| 37 | if (decimalPoint == 0) { |
| 38 | return '0'; |
| 39 | } |
| 40 | final numberFormat = NumberFormat() |
| 41 | ..maximumFractionDigits = decimalPoint |
| 42 | ..minimumFractionDigits = 1; |
| 43 | return numberFormat |
| 44 | .format( |
| 45 | DecimalIntl( |
| 46 | _bigIntDivision( |
| 47 | amount: amount, |
| 48 | divider: BigInt.from(pow(10, decimalPoint)), |
| 49 | ), |
| 50 | ), |
| 51 | ) |
| 52 | .replaceAll(',', ''); |
| 53 | } |
| 54 | |
| 55 | static double intAmountToDouble(int amount, [int decimalPoint = defaultDecimalPoint]) => |
| 56 | _bigIntDivision( |
| 57 | amount: BigInt.from(amount), |
| 58 | divider: BigInt.from(pow(10, decimalPoint)), |
| 59 | ).toDouble(); |
| 60 | |
| 61 | static int parseAmount(String amount, [int decimalPoint = defaultDecimalPoint]) { |
| 62 | final resultBigInt = |
| 63 | (Decimal.parse(amount) * Decimal.fromBigInt(BigInt.from(10).pow(decimalPoint))).toBigInt(); |
| 64 | if (!resultBigInt.isValidInt) { |
| 65 | try { |
| 66 | Fluttertoast.showToast( |
| 67 | msg: |
| 68 | 'Cannot transfer $amount. Maximum is ${intAmountToString(resultBigInt.toInt(), decimalPoint)}.'); |
| 69 | } catch (_) {} |
| 70 | } |
| 71 | return resultBigInt.toInt(); |
| 72 | } |
| 73 | |
| 74 | static BigInt bigIntFromDynamic(dynamic d) { |
| 75 | if (d is int) { |
| 76 | return BigInt.from(d); |
| 77 | } else if (d is BigInt) { |
| 78 | return d; |
| 79 | } else if (d == null) { |
| 80 | return BigInt.zero; |
| 81 | } else { |
| 82 | printV(('cannot cast value of type ${d.runtimeType} to BigInt')); |
| 83 | throw 'cannot cast value of type ${d.runtimeType} to BigInt'; |
| 84 | //return BigInt.zero; |
| 85 | } |
| 86 | } |
| 87 | } |