2021-12-24 12:52:08 +00:00
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
import 'package:cw_core/crypto_amount_format.dart';
|
|
|
|
|
|
|
|
const bitcoinAmountLength = 8;
|
|
|
|
const bitcoinAmountDivider = 100000000;
|
2024-02-20 17:02:52 +00:00
|
|
|
const lightningAmountDivider = 1;
|
2021-12-24 12:52:08 +00:00
|
|
|
final bitcoinAmountFormat = NumberFormat()
|
|
|
|
..maximumFractionDigits = bitcoinAmountLength
|
|
|
|
..minimumFractionDigits = 1;
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
String bitcoinAmountToString({required int amount}) => bitcoinAmountFormat.format(
|
2021-12-24 12:52:08 +00:00
|
|
|
cryptoAmountToDouble(amount: amount, divider: bitcoinAmountDivider));
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
double bitcoinAmountToDouble({required int amount}) =>
|
2021-12-24 12:52:08 +00:00
|
|
|
cryptoAmountToDouble(amount: amount, divider: bitcoinAmountDivider);
|
|
|
|
|
|
|
|
int stringDoubleToBitcoinAmount(String amount) {
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
try {
|
2023-02-15 21:16:21 +00:00
|
|
|
result = (double.parse(amount) * bitcoinAmountDivider).round();
|
2021-12-24 12:52:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2024-02-20 17:02:52 +00:00
|
|
|
|
|
|
|
String bitcoinAmountToLightningString({required int amount}) {
|
|
|
|
String formattedAmount = bitcoinAmountFormat.format(cryptoAmountToDouble(amount: amount, divider: lightningAmountDivider));
|
|
|
|
return formattedAmount.substring(0, formattedAmount.length - 2);
|
|
|
|
}
|
2024-02-27 21:50:26 +00:00
|
|
|
|
2024-03-01 17:08:32 +00:00
|
|
|
String satsToLightningString(int sats) {
|
2024-02-27 21:50:26 +00:00
|
|
|
String formattedAmount = bitcoinAmountFormat.format(sats);
|
|
|
|
return formattedAmount.substring(0, formattedAmount.length - 2);
|
|
|
|
}
|