[英]chaining to cast type in javascript
let finalPrice = room.points[0].price.finalPrice //string
finalPrice = +finalPrice //number
finalPrice = finalPrice.toFixed(2) //2 decimal
Is it possible to shorten above type casting?是否可以缩短上述类型的铸造?
if I do如果我做
+room.points[0].price.finalPrice.toFixed(2)
I'll get this error: toFixed is not a function
我会得到这个错误:
toFixed is not a function
Like this:像这样:
const finalPrice = Number(room.points[0].price.finalPrice).toFixed(2);
No need for intermediate variables.不需要中间变量。
The problem with your +
is that it has lower operator precedence than the .
您的
+
的问题在于它的运算符优先级低于.
with the .toFixed
call.使用
.toFixed
调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.