Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

javascript 如何实现保留两位小数(不四舍五入)


12.19888 => 12.19
12 => 12.00


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
function getResult (a) {
  return (Math.floor(a * 100) / 100).toFixed(2)
}

在JS中数字12.00和12是相同的值,因为不区分整数小数。所以保留2位小数只能是转为字符串形式表达。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...