javascript - 如何在 Intl.NumberFormat 中获取正数的前导 '+'?

标签 javascript angular typescript ecma

我正在使用 Intl.NumberFormat 将数字类型转换为 Angular2 中 typescript/javascript 中的格式化字符串。我想要一个 native 解决方案,这是理想的,但我需要一个前导加号来表示正数。

如果 Intl.NumberFormat 无法做到这一点,我还能如何本地做到这一点?

@Input() amount : number;

drawLabel() {
   var formatter = new Intl.NumberFormat("en-GB",
                                         {
                                            style: "decimal",
                                            minimumFractionDigits:1
                                         });
   ...

   this.label = formatter.format(this.amount)
}

最佳答案

2019 年你会这样做:

var formatter = new Intl.NumberFormat("en-GB", { style: "decimal",  signDisplay: 'always' });

console.log(formatter.format(-100.123456));
// output -100.123
console.log(formatter.format(100.123456));
// output +100.123
console.log(formatter.format(0));
// output +0.

关于javascript - 如何在 Intl.NumberFormat 中获取正数的前导 '+'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42086681/

相关文章:

javascript - Bootstrap slider 控件?

typescript - 在 Angular 2 中以两种方式绑定(bind)组件的属性

javascript - Angular 2 中的打印 Html 模板(Angular 2 中的 ng-print)

javascript - 如何在 Angular 6 中注入(inject)/集成第三​​方库?

reactjs - 将其存储在 useRef Hook 中,并将可变值保留在 '.current' 属性中

node.js - 类型错误 : Cannot read properties of undefined (reading 'listen' ) when testing with Jest, super 测试、Express、Typescript

javascript - 如何在 Haml 中使用 JavaScript 变量?

javascript - 如何在鼠标滚轮上使用 jquery 调用一次函数?

Javascript 在表的最后一行之前添加行到表中

cordova - ionic 2.1.8 中无法获取//__ion-dev-server/ionic_lab.html