我曾尝试使用toString()
和按位运算符|
,但我希望使用精度和字符串输出。正如@trincot所述,这是由于精度值造成的,例如-0.45将导致为-0。以下是我的代码。
typeof predictedHours //number e.g. -0.45
precision = 0
value={predictedHours.toFixed(precision)}
typeof value //string e.g. "-0"
问题 - 在这一行中是否有一句话的代码可以将-0转换为0 - value={predictedHours.toFixed(precision)}
?
-0.001.toFixed(2) === "-0"
触发的? - trincot-0.001.toFixed(2).replace(/^-0$/, "0")
。 - trincot