We have a flutter text field whereby a user can enter an amount as an int, and what we want to do is that int calculated an amount less a fee. Probably is I cannot work out here what I am doing wrong
TextFormField(
controller: _offerAmountController,
textInputAction: TextInputAction.next,
decoration: const InputDecoration(
hintText: '5-9999',
prefixIcon: Icon(Icons.attach_money_rounded),
// labelText: 'Your Offer',
),
keyboardType: TextInputType.number,
onChanged: (offerAmount) {
double sum = int.parse(offerAmount) * 0.1;
_offer = sum as int;
}),
Text(formatCurrency.format(_offer),
style: Theme.of(context)
.textTheme
.headlineSmall
.copyWith(color: Colors.blue[700]))
final formatCurrency =
NumberFormat.simpleCurrency(locale: 'en_AU', decimalDigits: 0);
CodePudding user response:
Try this:
setState(() {
_offer = sum.toInt();
});
CodePudding user response:
Try this
setState(() {
_offer = (sum).round();
});
or this
setState(() {
_offer = sum.toInt();
});
You can also try this if you need below code
double d = 50.5;
int i = d.toInt(); // i = 50
int i = d.round(); // i = 51
int i = d.ceil(); // i = 51
int i = d.floor(); // i = 50