Skip to content

Commit

Permalink
fix: Set floor instead to ceil for decimal values
Browse files Browse the repository at this point in the history
  • Loading branch information
bibash28 committed Apr 24, 2024
1 parent 1957f34 commit 3fbc4b7
Show file tree
Hide file tree
Showing 19 changed files with 48 additions and 48 deletions.
11 changes: 11 additions & 0 deletions lib/app/shared/extension/double_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extension DoubleExtension on double {
String decimalNumber(int n) {
int number = 1;
for (int i = 0; i < n; i++) {
number *= 10;
}

final twoDecimalNumber = (this * number).floor() / number;
return twoDecimalNumber.toString();
}
}
1 change: 1 addition & 0 deletions lib/app/shared/extension/extension.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export 'bigint_extension.dart';
export 'credential_status.dart';
export 'double_extension.dart';
export 'iterable_extension.dart';
export 'string_extension.dart';
export 'unit8List_extension.dart';
2 changes: 1 addition & 1 deletion lib/app/shared/extension/string_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

extension StringExtension on String {
String formatNumber() {
String get formatNumber {
if (isEmpty || length < 3) return this;
final formatter = NumberFormat('#,###');
final value = this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class _OperationViewState extends State<OperationView> {
),
const SizedBox(height: Sizes.spaceSmall),
MyText(
'''${state.amount.toStringAsFixed(6).formatNumber()} ${symbol ?? ''}''',
'''${state.amount.decimalNumber(6).formatNumber} ${symbol ?? ''}''',
textAlign: TextAlign.center,
style: Theme.of(context)
.textTheme
Expand Down
10 changes: 5 additions & 5 deletions lib/dashboard/connection/operation/widgets/fee_details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class FeeDetails extends StatelessWidget {
),
const Spacer(),
Text(
'${amount.toStringAsFixed(6).formatNumber()} $symbol',
'${amount.decimalNumber(6).formatNumber} $symbol',
style: Theme.of(context).textTheme.bodySmall,
),
],
Expand All @@ -50,7 +50,7 @@ class FeeDetails extends StatelessWidget {
),
const Spacer(),
Text(
'${fee.toStringAsFixed(6).formatNumber()} $symbol',
'${fee.decimalNumber(6).formatNumber} $symbol',
style: Theme.of(context).textTheme.bodySmall,
),
],
Expand All @@ -69,15 +69,15 @@ class FeeDetails extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
'''${grandTotal.toStringAsFixed(6).formatNumber()} $symbol''',
'''${grandTotal.decimalNumber(6).formatNumber} $symbol''',
style: Theme.of(context).textTheme.bodySmall,
),
if (tokenUSDRate > 0)
Text(
r'$' +
(grandTotal * tokenUSDRate)
.toStringAsFixed(6)
.formatNumber(),
.decimalNumber(6)
.formatNumber,
style: Theme.of(context).textTheme.bodySmall2,
),
],
Expand Down
2 changes: 1 addition & 1 deletion lib/dashboard/home/home/widgets/token_reward_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class TokenRewardDialog extends StatelessWidget {
children: [
TextSpan(
text:
''' ${tokenReward.amount.toString().formatNumber()} ${tokenReward.symbol} ''',
''' ${tokenReward.amount.toString().formatNumber} ${tokenReward.symbol} ''',
style:
Theme.of(context).textTheme.defaultDialogBody.copyWith(
fontWeight: FontWeight.w900,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class _ConfirmWithdrawalViewState extends State<ConfirmWithdrawalView> {
}
if (state.status == AppStatus.success) {
final amountAndSymbol =
'''${widget.isNFT ? widget.amount.toInt() : state.totalAmount.toStringAsFixed(getDecimalsToShow(state.totalAmount)).formatNumber()} ${widget.isNFT ? '${widget.selectedToken.symbol} #${widget.selectedToken.tokenId}' : widget.selectedToken.symbol}''';
'''${widget.isNFT ? widget.amount.toInt() : state.totalAmount.decimalNumber(getDecimalsToShow(state.totalAmount)).formatNumber} ${widget.isNFT ? '${widget.selectedToken.symbol} #${widget.selectedToken.tokenId}' : widget.selectedToken.symbol}''';
TransactionDoneDialog.show(
context: context,
amountAndSymbol: amountAndSymbol,
Expand All @@ -151,7 +151,7 @@ class _ConfirmWithdrawalViewState extends State<ConfirmWithdrawalView> {
},
builder: (context, state) {
final amountAndSymbol =
'''${widget.isNFT ? widget.amount.toInt() : state.tokenAmount.toStringAsFixed(getDecimalsToShow(state.tokenAmount)).formatNumber()} ${widget.isNFT ? '${widget.selectedToken.symbol} #${widget.selectedToken.tokenId}' : widget.selectedToken.symbol}''';
'''${widget.isNFT ? widget.amount.toInt() : state.tokenAmount.decimalNumber(getDecimalsToShow(state.tokenAmount)).formatNumber} ${widget.isNFT ? '${widget.selectedToken.symbol} #${widget.selectedToken.tokenId}' : widget.selectedToken.symbol}''';
return BasePage(
scrollView: false,
title: l10n.confirm,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ class ConfirmTransactionDetailsCard extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
'''${isNFT ? grandTotal.toInt() : grandTotal.toStringAsFixed(getDecimalsToShow(grandTotal)).formatNumber()} $symbol''',
'''${isNFT ? grandTotal.toInt() : grandTotal.decimalNumber(getDecimalsToShow(grandTotal)).formatNumber} $symbol''',
style: Theme.of(context).textTheme.bodySmall,
),
if (tokenUSDRate > 0)
Text(
r'$' +
(grandTotal * tokenUSDRate)
.toStringAsFixed(2)
.formatNumber(),
.decimalNumber(2)
.formatNumber,
style: Theme.of(context).textTheme.bodySmall2,
),
],
Expand All @@ -83,15 +83,13 @@ class ConfirmTransactionDetailsCard extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
'''${networkFee!.fee.toStringAsFixed(6).formatNumber()} ${networkFee!.tokenSymbol}''',
'''${networkFee!.fee.decimalNumber(6).formatNumber} ${networkFee!.tokenSymbol}''',
style: Theme.of(context).textTheme.bodySmall,
),
if (tokenUSDRate > 0 && networkFee?.tokenSymbol == symbol)
Text(
r'$' +
networkFee!.feeInUSD
.toStringAsFixed(2)
.formatNumber(),
networkFee!.feeInUSD.decimalNumber(2).formatNumber,
style: Theme.of(context).textTheme.bodySmall2,
),
],
Expand All @@ -111,15 +109,13 @@ class ConfirmTransactionDetailsCard extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
'''${isNFT ? amount.toInt() : amount.toStringAsFixed(getDecimalsToShow(amount)).formatNumber()} $symbol''',
'''${isNFT ? amount.toInt() : amount.decimalNumber(getDecimalsToShow(amount)).formatNumber} $symbol''',
style: Theme.of(context).textTheme.bodySmall,
),
if (tokenUSDRate > 0)
Text(
r'$' +
(amount * tokenUSDRate)
.toStringAsFixed(2)
.formatNumber(),
(amount * tokenUSDRate).decimalNumber(2).formatNumber,
style: Theme.of(context).textTheme.bodySmall2,
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ class InsertWithdrawalPageCubit extends Cubit<InsertWithdrawalPageState> {
InsertWithdrawalPageCubit({
required this.defaultSelectedToken,
}) : super(
InsertWithdrawalPageState(
selectedToken: defaultSelectedToken,
),
InsertWithdrawalPageState(selectedToken: defaultSelectedToken),
);

final TokenModel defaultSelectedToken;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class TokenAmountCalculatorState extends Equatable {
String? amount,
double? validAmount,
double? insertedAmount,
TokenModel? selectedToken,
}) {
return TokenAmountCalculatorState(
amount: amount ?? this.amount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ class _InsertWithdrawalAmountViewState
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(
height: Sizes.spaceSmall,
),
const SizedBox(height: Sizes.spaceSmall),
TokenSelectBoxView(selectedToken: state.selectedToken),
TokenAmountCalculatorView(selectedToken: state.selectedToken),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class _TokenAmountCalculatorPageState extends State<TokenAmountCalculatorPage> {

void _setAmountControllerText(String text) {
// no need to format when text end with .
amountController.text = text.endsWith('.') ? text : text.formatNumber();
amountController.text = text.endsWith('.') ? text : text.formatNumber;
amountController.selection = TextSelection.fromPosition(
TextPosition(offset: amountController.text.length),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ class _TokenSelectBoxItem extends StatelessWidget {
size: Sizes.icon,
color: Theme.of(context).colorScheme.inversePrimary,
),
const Spacer(
flex: 1,
),
const Spacer(flex: 1),
MyText(
tokenModel.calculatedBalance,
minFontSize: 10,
Expand All @@ -130,6 +128,7 @@ class _TokenSelectBoxItem extends StatelessWidget {
style: Theme.of(context).textTheme.bodySmall,
overflow: TextOverflow.ellipsis,
),
const SizedBox(width: 5),
MyText(
tokenModel.symbol,
minFontSize: 10,
Expand All @@ -143,8 +142,8 @@ class _TokenSelectBoxItem extends StatelessWidget {
MyText(
r'$' +
selectedToken.balanceInUSD
.toStringAsFixed(2)
.formatNumber(),
.decimalNumber(2)
.formatNumber,
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Theme.of(context).colorScheme.greyText,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class UsdValueText extends StatelessWidget {
width: Sizes.spaceXSmall,
),
Text(
r'$' + usdValue.toStringAsFixed(2).formatNumber(),
r'$' + usdValue.decimalNumber(2).formatNumber,
style: Theme.of(context).textTheme.bodySmall,
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ class _SendReceiveHomePageView extends StatelessWidget {
children: [
MyText(
state.selectedToken.calculatedBalanceInDouble
.toStringAsFixed(
.decimalNumber(
state.selectedToken.decimalsToShow,
)
.formatNumber(),
.formatNumber,
style: Theme.of(context).textTheme.headlineMedium,
maxLength: 12,
),
Expand All @@ -171,8 +171,8 @@ class _SendReceiveHomePageView extends StatelessWidget {
MyText(
r'$' +
state.selectedToken.balanceInUSD
.toStringAsFixed(2)
.formatNumber(),
.decimalNumber(2)
.formatNumber,
style: Theme.of(context).textTheme.normal,
),
const SizedBox(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ class TransactionItem extends StatelessWidget {
),
Text(
tokenUsdPrice != null
? (tokenUsdPrice! * amount)
.toStringAsFixed(2)
.formatNumber() +
? (tokenUsdPrice! * amount).decimalNumber(2).formatNumber +
r'$'
: r'$--.--',
style: Theme.of(context).textTheme.bodySmall2,
Expand Down Expand Up @@ -122,7 +120,7 @@ class TransactionItem extends StatelessWidget {
),
Expanded(
child: MyText(
'''${amount.toStringAsFixed(amount < 1 ? 5 : 2).formatNumber()} '''
'''${amount.decimalNumber(amount < 1 ? 5 : 2).formatNumber} '''
'$symbol',
minFontSize: 8,
textAlign: TextAlign.end,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TokenItem extends StatelessWidget {
child: MyText(
isSecure
? '****'
: ('''${token.calculatedBalanceInDouble.toStringAsFixed(token.decimalsToShow).formatNumber()} ${token.symbol}'''),
: ('''${token.calculatedBalanceInDouble.decimalNumber(token.decimalsToShow).formatNumber} ${token.symbol}'''),
style: Theme.of(context)
.textTheme
.listTileTitle
Expand All @@ -63,7 +63,7 @@ class TokenItem extends StatelessWidget {
isSecure
? '****'
: (r'$' +
token.balanceInUSD.toStringAsFixed(2).formatNumber()),
token.balanceInUSD.decimalNumber(2).formatNumber),
style: Theme.of(context).textTheme.listTileSubtitle,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class TotalWalletBalance extends StatelessWidget {
child: MyText(
state.isSecure
? '****'
: '''${state.totalBalanceInUSD.toStringAsFixed(2).formatNumber()} \$''',
: '''${state.totalBalanceInUSD.decimalNumber(2).formatNumber} \$''',
minFontSize: 8,
style: Theme.of(context).textTheme.headlineMedium,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ class SelectNetworkFeeItem extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.end,
children: [
MyText(
'''${networkFeeModel.fee.toString().formatNumber()} ${networkFeeModel.tokenSymbol}''',
'''${networkFeeModel.fee.toString().formatNumber} ${networkFeeModel.tokenSymbol}''',
style: Theme.of(context).textTheme.bodySmall,
),
const SizedBox(
height: Sizes.space2XSmall,
),
MyText(
'''\$${networkFeeModel.feeInUSD == 0.0 ? '--.--' : networkFeeModel.feeInUSD.toStringAsFixed(4).formatNumber()}''',
'''\$${networkFeeModel.feeInUSD == 0.0 ? '--.--' : networkFeeModel.feeInUSD.decimalNumber(4).formatNumber}''',
style: Theme.of(context).textTheme.bodySmall2,
),
],
Expand Down

0 comments on commit 3fbc4b7

Please sign in to comment.