简体   繁体   English

在javascript中链接到类型转换

[英]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.

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