无法解决货币中的数字格式?
not able to solve number format in currency?
我试过 NumberFormat "#,##0.##"
我试过 NumberFormat "#,##0.00"
我试过 NumberFormat "#,##0.#0"
我要的格式是(123,4567.89)
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:intl/intl.dart';
class CurrencyFormat extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
// TODO: implement formatEditUpdate
if (newValue.selection.baseOffset == 0) {
return newValue;
}
double value = double.parse(newValue.text);
final money = NumberFormat("#,###.##", "en_US");
String newText = money.format(value);
return newValue.copyWith(
text: newText,
selection: TextSelection.collapsed(offset: newText.length));
}
}
我在 textformfield 小部件中调用此货币格式
'''
Widget mrpTextField() {
return Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
keyboardType: TextInputType.number,
inputFormatters: [
FilteringTextInputFormatter.digitsOnly,
CurrencyFormat()
],
controller: _mrp,
validator: (value) {
if (value.isEmpty) {
return "MRP";
}else if (value.length > 6) {
return "";
}
return null;
},
decoration: InputDecoration(
labelText: "Enter MRP",
),
maxLines: null,
),
);
}
'''
在 TextFormField
中将 FilteringTextInputFormatter.digitsOnly,
更改为 FilteringTextInputFormatter.allow(RegExp("[0-9\.]")),
在CurrencyFormat
class CurrencyFormat extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
if (newValue.text.isEmpty) {
return newValue.copyWith(text: '');
} else if (newValue.text == ".") {
return newValue.copyWith(
text: '0.00',
selection: TextSelection.collapsed(offset: 2),
);
} else {
NumberFormat f = NumberFormat("#,###0.00", "en_US");
double newNumber = 0;
if ((!newValue.text.contains(".")) && oldValue.text.contains('.')) {
String tempString = newValue.text.replaceAll(f.symbols.GROUP_SEP, '');
tempString = tempString.substring(0, tempString.length - 2) +
"." +
tempString.substring(tempString.length - 2);
newNumber = double.parse(tempString);
} else {
newNumber = double.parse(newValue.text
.replaceAll(f.symbols.GROUP_SEP, '')
.replaceAll("..", '.'));
}
String newString = f.format(newNumber);
int cursorPosition = 0;
if (oldValue.text.length > newString.length) {
cursorPosition = -1;
} else if (oldValue.text.length < newString.length) {
cursorPosition = 1;
} else {
if (oldValue.text.replaceAll(f.symbols.GROUP_SEP, '').length >
newValue.text.replaceAll(f.symbols.GROUP_SEP, '').length) {
cursorPosition = -1;
if (newString == "0.00" && oldValue.selection.baseOffset == 0) {
newString = "";
}
} else if (oldValue.text.replaceAll(f.symbols.GROUP_SEP, '').length <
newValue.text.replaceAll(f.symbols.GROUP_SEP, '').length) {
cursorPosition = 1;
} else if (oldValue.selection.extentOffset >
oldValue.selection.baseOffset) {
cursorPosition =
oldValue.selection.baseOffset - oldValue.selection.extentOffset;
newString =
newString.substring(0, oldValue.selection.baseOffset - 1) +
newString.substring(oldValue.selection.baseOffset + 1);
newNumber = double.parse(newString
.replaceAll(f.symbols.GROUP_SEP, '')
.replaceAll("..", '.'));
newString = f.format(newNumber);
if (newString == "0.00" && oldValue.selection.baseOffset == 0) {
newString = "";
}
}
}
return TextEditingValue(
text: newString,
selection: TextSelection.collapsed(
offset: oldValue.selection.extent.offset +
cursorPosition +
(f.symbols.GROUP_SEP.allMatches(newString).length -
f.symbols.GROUP_SEP.allMatches(oldValue.text).length)),
);
}
}
}
同样,"#,###0.00"
是对的
我试过 NumberFormat "#,##0.##"
我试过 NumberFormat "#,##0.00"
我试过 NumberFormat "#,##0.#0"
我要的格式是(123,4567.89)
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:intl/intl.dart';
class CurrencyFormat extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
// TODO: implement formatEditUpdate
if (newValue.selection.baseOffset == 0) {
return newValue;
}
double value = double.parse(newValue.text);
final money = NumberFormat("#,###.##", "en_US");
String newText = money.format(value);
return newValue.copyWith(
text: newText,
selection: TextSelection.collapsed(offset: newText.length));
}
}
我在 textformfield 小部件中调用此货币格式
'''
Widget mrpTextField() {
return Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
keyboardType: TextInputType.number,
inputFormatters: [
FilteringTextInputFormatter.digitsOnly,
CurrencyFormat()
],
controller: _mrp,
validator: (value) {
if (value.isEmpty) {
return "MRP";
}else if (value.length > 6) {
return "";
}
return null;
},
decoration: InputDecoration(
labelText: "Enter MRP",
),
maxLines: null,
),
);
}
'''
在 TextFormField
中将 FilteringTextInputFormatter.digitsOnly,
更改为 FilteringTextInputFormatter.allow(RegExp("[0-9\.]")),
在CurrencyFormat
class CurrencyFormat extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
if (newValue.text.isEmpty) {
return newValue.copyWith(text: '');
} else if (newValue.text == ".") {
return newValue.copyWith(
text: '0.00',
selection: TextSelection.collapsed(offset: 2),
);
} else {
NumberFormat f = NumberFormat("#,###0.00", "en_US");
double newNumber = 0;
if ((!newValue.text.contains(".")) && oldValue.text.contains('.')) {
String tempString = newValue.text.replaceAll(f.symbols.GROUP_SEP, '');
tempString = tempString.substring(0, tempString.length - 2) +
"." +
tempString.substring(tempString.length - 2);
newNumber = double.parse(tempString);
} else {
newNumber = double.parse(newValue.text
.replaceAll(f.symbols.GROUP_SEP, '')
.replaceAll("..", '.'));
}
String newString = f.format(newNumber);
int cursorPosition = 0;
if (oldValue.text.length > newString.length) {
cursorPosition = -1;
} else if (oldValue.text.length < newString.length) {
cursorPosition = 1;
} else {
if (oldValue.text.replaceAll(f.symbols.GROUP_SEP, '').length >
newValue.text.replaceAll(f.symbols.GROUP_SEP, '').length) {
cursorPosition = -1;
if (newString == "0.00" && oldValue.selection.baseOffset == 0) {
newString = "";
}
} else if (oldValue.text.replaceAll(f.symbols.GROUP_SEP, '').length <
newValue.text.replaceAll(f.symbols.GROUP_SEP, '').length) {
cursorPosition = 1;
} else if (oldValue.selection.extentOffset >
oldValue.selection.baseOffset) {
cursorPosition =
oldValue.selection.baseOffset - oldValue.selection.extentOffset;
newString =
newString.substring(0, oldValue.selection.baseOffset - 1) +
newString.substring(oldValue.selection.baseOffset + 1);
newNumber = double.parse(newString
.replaceAll(f.symbols.GROUP_SEP, '')
.replaceAll("..", '.'));
newString = f.format(newNumber);
if (newString == "0.00" && oldValue.selection.baseOffset == 0) {
newString = "";
}
}
}
return TextEditingValue(
text: newString,
selection: TextSelection.collapsed(
offset: oldValue.selection.extent.offset +
cursorPosition +
(f.symbols.GROUP_SEP.allMatches(newString).length -
f.symbols.GROUP_SEP.allMatches(oldValue.text).length)),
);
}
}
}
同样,"#,###0.00"
是对的