使用 Intl.NumberFormat [duplicate] 将格式四舍五入

format getting rounded off by a large amount using Intl.NumberFormat [duplicate]

提问人:anjanesh 提问时间:9/21/2023 更新时间:9/21/2023 访问量:23

问:

let just_the_number = '8973565';

const just_the_number_formatted = Intl.NumberFormat("en-IN", { minimumFractionDigits: 0 }).format(parseFloat(just_the_number));
console.log(just_the_number_formatted)

let formatted_amount = new Intl.NumberFormat("en-IN", {
      style: "currency",
      currency: "INR",
      notation: "compact",
      compactDisplay: "long",
    }).format(just_the_number);

console.log(formatted_amount) // prints ₹90L

formatted_amount在这里得到了很大的四舍五入。 我想要上面打印的 ₹89.73L

JavaScript 格式 十进制 货币

评论


答:

0赞 Jarmo T 9/21/2023 #1

您必须使用 和 选项来自定义小数位的行为。minimumFractionDigitsmaximumFractionDigits

请注意,它仍然会四舍五入。

例如:

let just_the_number = '8973565';

let formatted_amount = new Intl.NumberFormat("en-IN", {
      style: "currency",
      currency: "INR",
      notation: "compact",
      compactDisplay: "long",
      minimumFractionDigits: 2,
      maximumFractionDigits: 2,
    }).format(just_the_number);

console.log(formatted_amount) // prints ₹89.74L