[英]chaining to cast type in javascript
let finalPrice = room.points[0].price.finalPrice //string
finalPrice = +finalPrice //number
finalPrice = finalPrice.toFixed(2) //2 decimal
是否可以縮短上述類型的鑄造?
如果我做
+room.points[0].price.finalPrice.toFixed(2)
我會得到這個錯誤: toFixed is not a function
像這樣:
const finalPrice = Number(room.points[0].price.finalPrice).toFixed(2);
不需要中間變量。
您的+
的問題在於它的運算符優先級低於.
使用.toFixed
調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.