如何在flutter的Numberformat.compactCurreny中强制使用小数?

问题描述 投票:0回答:1

我正在尝试获得人类综合货币价值。

如果值为 100000,则返回 100K 当我想得到 100.5K 时,值 100500 会返回 101K

请注意,我使用 intl 数字格式,因为我希望能够发送区域设置并针对不同区域正确解析点和逗号

这是我正在运行的代码片段:

  double amount = 100500;
  String currency = "EUR";
  String locale = "en-US";

  NumberFormat.compactCurrency(
    locale: locale,
    name: currency,
    symbol: "",
    decimalDigits: 1,
  ).format(amount);

似乎调用

compactCurrency
基本上将数字压缩为 3 个字符的字符串,因此它对值进行上取/下取而不是显示与其对应的小数

这种行为有什么解决办法吗?我已经看到了创建新区域设置的选项,但这并不是一个真正优雅的解决方案,因为它用于显示每个区域设置的不同模式,并且必须维护分隔符逻辑和所有内容。

flutter dart currency number-formatting
1个回答
0
投票

设置属性

maximumFractionDigits
可以解决问题:

  double amount = 100500;
  String currency = "EUR";
  String locale = "en-US";

  (NumberFormat.compactCurrency(
      locale: locale,
      name: currency,
      symbol: "",
      decimalDigits: 1,
    )..maximumFractionDigits = 1)
        .format(amount);
// = '100.5K'
© www.soinside.com 2019 - 2024. All rights reserved.