繁体   English   中英

使用toFixed和角数过滤器有什么区别?

[英]What's the difference between using toFixed and the angular number filter?

有什么区别:

{{3.14159 | number:2}}和{{3.14159.toFixed(2)}}

有人提供优势吗? 谢谢

这里的价值仍然相同,但掩盖为3.14

{{ 3.14159 | number : 2 }} 

但在这里

{{ 3.14159.toFixed(2) }}

toFixed(x)函数将数字转换为字符串,只保留两位小数。 例如

var num = 5.56789;
var n = num.toFixed();
// the output is = 6

如果你使用

var num = 5.56789;
var n = num.toFixed(2);
// the output is = 5.57

注意:如果所需的小数位数高于实际数字,则添加空值以创建所需的小数位数。

角数过滤器不会更改属性的原始值,例如:

{{ 3.14159 | number : 2 }} // this will give you 3.14 in the dom but the actual value will still be 3.14159

当您使用过滤器时,它仅用于显示目的,并不会更改它只是屏蔽它的属性。 当您使用toFixed()您将返回一个设置为指定小数位的原始数字的字符串,然后可以将其设置为另一个变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM