mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-10-17 05:41:29 +02:00
CW-1155: Mask fiat amounts to be more readable (#2420)
* feat: Mask fiat amounts to be more readable, with comma formating for large numbers * fix: Add fiat masking to buy and sell
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'package:cake_wallet/buy/buy_provider.dart';
|
||||
import 'package:cake_wallet/buy/payment_method.dart';
|
||||
import 'package:cake_wallet/core/selectable_option.dart';
|
||||
import 'package:cake_wallet/entities/calculate_fiat_amount.dart';
|
||||
import 'package:cake_wallet/entities/fiat_currency.dart';
|
||||
import 'package:cake_wallet/entities/provider_types.dart';
|
||||
import 'package:cake_wallet/exchange/limits.dart';
|
||||
@@ -96,7 +97,7 @@ class Quote extends SelectableOption {
|
||||
|
||||
@override
|
||||
String get topLeftSubTitle =>
|
||||
this.rate > 0 ? '1 $cryptoName = ${rate.toStringAsFixed(2)} $fiatName' : '';
|
||||
this.rate > 0 ? '1 $cryptoName = ${formatWithCommas(rate.toStringAsFixed(2))} $fiatName' : '';
|
||||
|
||||
@override
|
||||
String get bottomLeftSubTitle {
|
||||
|
||||
@@ -4,7 +4,7 @@ String calculateFiatAmount({double? price, String? cryptoAmount}) {
|
||||
}
|
||||
|
||||
cryptoAmount = cryptoAmount.replaceAll(',', '.');
|
||||
|
||||
|
||||
final _amount = double.parse(cryptoAmount);
|
||||
final _result = price * _amount;
|
||||
final result = _result < 0 ? _result * -1 : _result;
|
||||
@@ -15,14 +15,31 @@ String calculateFiatAmount({double? price, String? cryptoAmount}) {
|
||||
|
||||
var formatted = '';
|
||||
final parts = result.toString().split('.');
|
||||
|
||||
|
||||
if (parts.length >= 2) {
|
||||
if (parts[1].length > 2) {
|
||||
formatted = parts[0] + '.' + parts[1].substring(0, 2);
|
||||
formatted = formatWithCommas(parts[0] + '.' + parts[1].substring(0, 2));
|
||||
} else {
|
||||
formatted = parts[0] + '.' + parts[1];
|
||||
formatted = formatWithCommas(parts[0] + '.' + parts[1]);
|
||||
}
|
||||
} else {
|
||||
formatted = formatWithCommas(parts[0]);
|
||||
}
|
||||
|
||||
return result > 0.01 ? formatted : '< 0.01';
|
||||
}
|
||||
|
||||
String formatWithCommas(String number) {
|
||||
if (number.isEmpty) return number;
|
||||
|
||||
final parts = number.split('.');
|
||||
final integerPart = parts[0];
|
||||
final decimalPart = parts.length > 1 ? parts[1] : '';
|
||||
|
||||
final formattedInteger = integerPart.replaceAllMapped(
|
||||
RegExp(r'\B(?=(\d{3})+(?!\d))'),
|
||||
(Match match) => ',',
|
||||
);
|
||||
|
||||
return decimalPart.isNotEmpty ? '$formattedInteger.$decimalPart' : formattedInteger;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'calculate_fiat_amount.dart';
|
||||
|
||||
String calculateFiatAmountRaw({required double cryptoAmount, double? price}) {
|
||||
if (price == null) {
|
||||
return '0.00';
|
||||
@@ -9,5 +11,5 @@ String calculateFiatAmountRaw({required double cryptoAmount, double? price}) {
|
||||
return '0.00';
|
||||
}
|
||||
|
||||
return result > 0.01 ? result.toStringAsFixed(2) : '< 0.01';
|
||||
return result > 0.01 ? formatWithCommas(result.toStringAsFixed(2)) : '< 0.01';
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
import 'calculate_fiat_amount.dart';
|
||||
|
||||
String formatAmount(String amount) {
|
||||
if ((!amount.contains('.'))&&(!amount.contains(','))) {
|
||||
return amount + '.00';
|
||||
return formatWithCommas(amount + '.00');
|
||||
} else if ((amount.endsWith('.'))||(amount.endsWith(','))) {
|
||||
return amount + '00';
|
||||
return formatWithCommas(amount + '00');
|
||||
}
|
||||
return amount;
|
||||
return formatWithCommas(amount);
|
||||
}
|
||||
Reference in New Issue
Block a user