mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2024-12-22 19:49:22 +00:00
feat: Int overflow issue (#1482)
Some checks failed
Cache Dependencies / test (push) Has been cancelled
Some checks failed
Cache Dependencies / test (push) Has been cancelled
This commit is contained in:
parent
ffa0b416e9
commit
fc2c9a2bcc
1 changed files with 17 additions and 9 deletions
|
@ -1,15 +1,13 @@
|
||||||
import 'package:intl/intl.dart';
|
import 'dart:math';
|
||||||
|
|
||||||
const evmChainAmountLength = 12;
|
|
||||||
const evmChainAmountDivider = 1000000000000;
|
|
||||||
final evmChainAmountFormat = NumberFormat()
|
|
||||||
..maximumFractionDigits = evmChainAmountLength
|
|
||||||
..minimumFractionDigits = 1;
|
|
||||||
|
|
||||||
class EVMChainFormatter {
|
class EVMChainFormatter {
|
||||||
|
static int _divider = 0;
|
||||||
|
|
||||||
static int parseEVMChainAmount(String amount) {
|
static int parseEVMChainAmount(String amount) {
|
||||||
try {
|
try {
|
||||||
return (double.parse(amount) * evmChainAmountDivider).round();
|
final decimalLength = _getDividerForInput(amount);
|
||||||
|
_divider = decimalLength;
|
||||||
|
return (double.parse(amount) * pow(10, decimalLength)).round();
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -17,9 +15,19 @@ class EVMChainFormatter {
|
||||||
|
|
||||||
static double parseEVMChainAmountToDouble(int amount) {
|
static double parseEVMChainAmountToDouble(int amount) {
|
||||||
try {
|
try {
|
||||||
return amount / evmChainAmountDivider;
|
return amount / pow(10, _divider);
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int _getDividerForInput(String amount) {
|
||||||
|
final result = amount.split('.');
|
||||||
|
if (result.length > 1) {
|
||||||
|
final decimalLength = result[1].length;
|
||||||
|
return decimalLength;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue