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
363 views
in Technique[技术] by (71.8m points)

javascript - 浮动后如何减少两位数? 在Java脚本中,无需更改为字符串(How to cut two digits after my float? In Java script without changing to string)

I want to take 2 digits after float like below example:(我想在浮点数后取2位数字,例如以下示例:)

function cut() { var num1=8.844 // should be return 8.84 var num2=8.847 // should be return 8.84 }   ask by Chanchhunneng chrea translate from so

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

1 Answer

0 votes
by (71.8m points)

You could multiply the value with 100, take the floorished value and divide by 100.(您可以将值乘以100,取下限值并除以100。)

You may witness the wonderful world of floating point arithmetic, where numbers have some more or less decimals, like Is floating point math broken?(您可能会见证浮点算术的奇妙世界,其中的数字或多或少都有小数,例如浮点算术是否损坏?) function cut(value) { return Math.floor(value * 100) / 100; } console.log(cut(8.844)); console.log(cut(8.847));

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

...