2021-01-05 18:31:03 +00:00
|
|
|
import 'dart:math';
|
|
|
|
|
2020-05-12 12:04:54 +00:00
|
|
|
import 'package:intl/intl.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/entities/crypto_amount_format.dart';
|
2020-05-12 12:04:54 +00:00
|
|
|
|
|
|
|
const bitcoinAmountLength = 8;
|
|
|
|
const bitcoinAmountDivider = 100000000;
|
|
|
|
final bitcoinAmountFormat = NumberFormat()
|
|
|
|
..maximumFractionDigits = bitcoinAmountLength
|
|
|
|
..minimumFractionDigits = 1;
|
|
|
|
|
2021-01-05 18:31:03 +00:00
|
|
|
String bitcoinAmountToString({int amount}) => bitcoinAmountFormat.format(
|
|
|
|
cryptoAmountToDouble(amount: amount, divider: bitcoinAmountDivider));
|
|
|
|
|
|
|
|
double bitcoinAmountToDouble({int amount}) =>
|
|
|
|
cryptoAmountToDouble(amount: amount, divider: bitcoinAmountDivider);
|
|
|
|
|
|
|
|
int stringDoubleToBitcoinAmount(String amount) {
|
|
|
|
final splitted = amount.split('');
|
|
|
|
final dotIndex = amount.indexOf('.');
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < splitted.length; i++) {
|
|
|
|
try {
|
|
|
|
if (dotIndex == i) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-05-12 12:04:54 +00:00
|
|
|
|
2021-01-05 18:31:03 +00:00
|
|
|
final char = splitted[i];
|
|
|
|
final multiplier = dotIndex < i
|
|
|
|
? bitcoinAmountDivider ~/ pow(10, (i - dotIndex))
|
|
|
|
: (bitcoinAmountDivider * pow(10, (dotIndex - i -1))).toInt();
|
|
|
|
final num = int.parse(char) * multiplier;
|
|
|
|
result += num;
|
|
|
|
} catch (_) {}
|
|
|
|
}
|
2020-09-21 11:50:26 +00:00
|
|
|
|
2021-01-05 18:31:03 +00:00
|
|
|
return result;
|
|
|
|
}
|