cake_wallet/cw_evm/lib/evm_chain_formatter.dart

34 lines
725 B
Dart
Raw Normal View History

2024-06-14 13:24:10 +00:00
import 'dart:math';
class EVMChainFormatter {
2024-06-14 13:24:10 +00:00
static int _divider = 0;
static int parseEVMChainAmount(String amount) {
try {
2024-06-14 13:24:10 +00:00
final decimalLength = _getDividerForInput(amount);
_divider = decimalLength;
return (double.parse(amount) * pow(10, decimalLength)).round();
} catch (_) {
return 0;
}
}
static double parseEVMChainAmountToDouble(int amount) {
try {
2024-06-14 13:24:10 +00:00
return amount / pow(10, _divider);
} catch (_) {
return 0;
}
}
2024-06-14 13:24:10 +00:00
static int _getDividerForInput(String amount) {
final result = amount.split('.');
if (result.length > 1) {
final decimalLength = result[1].length;
return decimalLength;
} else {
return 0;
}
}
}