flutter - 德语区域设置的 NumberFormat 奇怪的舍入行为
问题描述
我有一个颤动的 TextField,我想在其中输入德语数字(Decimalseparator: , - Thousandseparator: .)。为此,我使用 TextInputFormatter,在其中使用 NumberFormat。
但是 NumberFormat.format(1.9999) 会自动四舍五入为二。如何影响 Numberformat 或如何使用具有不同实现的 Textinput 实现德语区域设置?
class GermanDecimalInputFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
if (newValue.text == '')
return newValue.copyWith(
text: '0',
selection: newValue.selection
.copyWith(baseOffset: newValue.selection.baseOffset + 1));
String newText = newValue.text.replaceAll('.', '');
final deFormat = NumberFormat.decimalPattern(
'de',
);
try {
var number = deFormat.parse(newText);
newText = deFormat.format(number); // re-adds thousand separator
if (newValue.text.endsWith(',')) newText += ',';
} catch (e) {
return oldValue;
}
var shift = newValue.text.length - newText.length;
TextSelection newSelection = TextSelection.fromPosition(
TextPosition(offset: newValue.selection.baseOffset - shift));
return TextEditingValue(text: newText, selection: newSelection);
}
}
解决方案
我使用了包 number_display。
final display =
createDisplay(length: 100, separator: '.', decimalPoint: ',');
newText = display(number);
推荐阅读
- asp.net - 我的 VB.NET Web 应用程序身份验证问题
- discord.js - 从消息中删除所有反应
- javascript - 按 3 个变量(获胜、失败和种子)对小组中的球队进行排序
- git - 将一个 VS2019 项目推送到 GitHub
- microsoft-dynamics - 无法为 Power Apps 创建嵌入式画布应用,在租户中找不到
- c++ - C ++:将方法转换为另一种方法
- python - 使用 ArgumentParser 将参数保存在字典中
- vba - VBA 从文件路径中提取字符串
- typescript - 我正在使用柏树来自动化。我需要打开窗口文件上传对话框并附加任何文件。我该怎么做?
- c++ - 带参数的 QT 信号如何工作