Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - 使用JavaScript获取数字的小数部分(Get decimal portion of a number with JavaScript)

I have float numbers like 3.2 and 1.6 .(我有漂浮数字,如3.21.6 。)

I need to separate the number into the integer and decimal part.(我需要将数字分成整数和小数部分。) For example, a value of 3.2 would be split into two numbers, ie 3 and 0.2(例如,值3.2将被分成两个数字,即30.2) Getting the integer portion is easy:(获取整数部分很简单:) n = Math.floor(n); But I am having trouble getting the decimal portion.(但是我无法获得小数部分。) I have tried this:(我试过这个:) remainer = n % 2; //obtem a parte decimal do rating But it does not always work correctly.(但它并不总是正常工作。) The previous code has the following output:(前面的代码具有以下输出:) n = 3.1 => remainer = 1.1 What I am missing here?(我在这里缺少什么?)   ask by Oscar translate from so

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

1 Reply

0 votes
by (71.8m points)

Use 1 , not 2 .(使用1 ,而不是2 。)

js> 2.3 % 1 0.2999999999999998

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...