2024-03-16 10:55:03 +00:00
|
|
|
import 'dart:math';
|
|
|
|
|
2024-08-07 12:32:47 +00:00
|
|
|
import 'package:cw_zano/zano_wallet_api.dart';
|
2024-03-16 10:55:03 +00:00
|
|
|
import 'package:decimal/decimal.dart';
|
|
|
|
import 'package:decimal/intl.dart';
|
2024-08-07 12:32:47 +00:00
|
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
2024-03-16 10:55:03 +00:00
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
|
|
|
|
class ZanoFormatter {
|
|
|
|
static const defaultDecimalPoint = 12;
|
|
|
|
|
2024-08-07 12:32:47 +00:00
|
|
|
//static final numberFormat = NumberFormat()
|
|
|
|
// ..maximumFractionDigits = defaultDecimalPoint
|
|
|
|
// ..minimumFractionDigits = 1;
|
2024-03-16 10:55:03 +00:00
|
|
|
|
2024-08-07 12:32:47 +00:00
|
|
|
static Decimal _bigIntDivision({required BigInt amount, required BigInt divider}) {
|
|
|
|
return (Decimal.fromBigInt(amount) / Decimal.fromBigInt(divider)).toDecimal();
|
|
|
|
}
|
2024-03-16 10:55:03 +00:00
|
|
|
|
2024-08-07 12:32:47 +00:00
|
|
|
static String intAmountToString(int amount, [int decimalPoint = defaultDecimalPoint]) {
|
|
|
|
final numberFormat = NumberFormat()..maximumFractionDigits = decimalPoint
|
|
|
|
..minimumFractionDigits = 1;
|
|
|
|
return numberFormat.format(
|
2024-03-19 15:51:08 +00:00
|
|
|
DecimalIntl(
|
2024-03-20 11:17:48 +00:00
|
|
|
_bigIntDivision(
|
|
|
|
amount: BigInt.from(amount),
|
2024-03-19 15:51:08 +00:00
|
|
|
divider: BigInt.from(pow(10, decimalPoint)),
|
|
|
|
),
|
|
|
|
),
|
2024-03-20 11:17:48 +00:00
|
|
|
)
|
|
|
|
.replaceAll(',', '');
|
2024-08-07 12:32:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static String bigIntAmountToString(BigInt amount, [int decimalPoint = defaultDecimalPoint]) {
|
|
|
|
final numberFormat = NumberFormat()..maximumFractionDigits = decimalPoint
|
|
|
|
..minimumFractionDigits = 1;
|
|
|
|
return numberFormat.format(
|
2024-03-19 15:51:08 +00:00
|
|
|
DecimalIntl(
|
|
|
|
_bigIntDivision(
|
|
|
|
amount: amount,
|
|
|
|
divider: BigInt.from(pow(10, decimalPoint)),
|
|
|
|
),
|
|
|
|
),
|
2024-03-20 11:17:48 +00:00
|
|
|
)
|
|
|
|
.replaceAll(',', '');
|
2024-08-07 12:32:47 +00:00
|
|
|
}
|
2024-03-20 11:17:48 +00:00
|
|
|
|
|
|
|
static double intAmountToDouble(int amount, [int decimalPoint = defaultDecimalPoint]) => _bigIntDivision(
|
|
|
|
amount: BigInt.from(amount),
|
|
|
|
divider: BigInt.from(pow(10, decimalPoint)),
|
|
|
|
).toDouble();
|
|
|
|
|
2024-08-07 12:32:47 +00:00
|
|
|
static int parseAmount(String amount, [int decimalPoint = defaultDecimalPoint]) {
|
|
|
|
final resultBigInt = (Decimal.parse(amount) * Decimal.fromBigInt(BigInt.from(10).pow(decimalPoint))).toBigInt();
|
|
|
|
if (!resultBigInt.isValidInt) {
|
|
|
|
Fluttertoast.showToast(msg: 'Cannot transfer $amount. Maximum is ${intAmountToString(resultBigInt.toInt(), decimalPoint)}.');
|
|
|
|
}
|
|
|
|
return resultBigInt.toInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
static BigInt bigIntFromDynamic(dynamic d) {
|
|
|
|
if (d is int) {
|
|
|
|
return BigInt.from(d);
|
|
|
|
} else if (d is BigInt) {
|
|
|
|
return d;
|
|
|
|
} else if (d == null) {
|
|
|
|
return BigInt.zero;
|
|
|
|
} else {
|
|
|
|
ZanoWalletApi.error('cannot cast value of type ${d.runtimeType} to BigInt');
|
|
|
|
throw 'cannot cast value of type ${d.runtimeType} to BigInt';
|
|
|
|
//return BigInt.zero;
|
|
|
|
}
|
|
|
|
}
|
2024-03-16 10:55:03 +00:00
|
|
|
}
|