2024-03-16 10:55:03 +00:00
|
|
|
import 'dart:math';
|
|
|
|
|
|
|
|
import 'package:decimal/decimal.dart';
|
|
|
|
import 'package:decimal/intl.dart';
|
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
|
|
|
|
class ZanoFormatter {
|
|
|
|
static const defaultDecimalPoint = 12;
|
|
|
|
|
|
|
|
static final numberFormat = NumberFormat()
|
|
|
|
..maximumFractionDigits = defaultDecimalPoint
|
|
|
|
..minimumFractionDigits = 1;
|
|
|
|
|
2024-03-19 15:51:08 +00:00
|
|
|
static Decimal _bigIntDivision({required BigInt amount, required BigInt divider}) =>
|
|
|
|
(Decimal.fromBigInt(amount) / Decimal.fromBigInt(divider)).toDecimal();
|
2024-03-16 10:55:03 +00:00
|
|
|
|
2024-03-20 11:17:48 +00:00
|
|
|
static String intAmountToString(int amount, [int decimalPoint = defaultDecimalPoint]) => 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(',', '');
|
|
|
|
static String bigIntAmountToString(BigInt amount, [int decimalPoint = defaultDecimalPoint]) => 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(',', '');
|
|
|
|
|
|
|
|
static double intAmountToDouble(int amount, [int decimalPoint = defaultDecimalPoint]) => _bigIntDivision(
|
|
|
|
amount: BigInt.from(amount),
|
|
|
|
divider: BigInt.from(pow(10, decimalPoint)),
|
|
|
|
).toDouble();
|
|
|
|
|
|
|
|
static int parseAmount(String amount, [int decimalPoint = defaultDecimalPoint]) =>
|
|
|
|
(Decimal.parse(amount) * Decimal.fromBigInt(BigInt.from(10).pow(decimalPoint))).toBigInt().toInt();
|
2024-03-16 10:55:03 +00:00
|
|
|
}
|