简体   繁体   English

进行数学运算是否有原因* 1000/1000没有意义

[英]is there a reason for math.round *1000 /1000 doesn't make sense

I am looking at this line of code and I cannot make sense of it. 我正在看这行代码,我无法理解。 This particular code is javascript, but I eventually would like to make a java android app. 这个特定的代码是javascript,但我最终还是想制作一个Java android应用。

$("#TxtHalfDot").val(Math.round((60000/bpm)*3*1000)/1000); 

         //bpm being a user entered value

I understand the process of the math and have been through it with a calculator many times. 我了解数学的过程,并且已经使用计算器进行了很多次。 However, I can not make sense of the *1000 followed by /1000. 但是,我无法理解* 1000后跟/ 1000。

My Question 我的问题

Is this a strange behavior of the "math.round" function or is it just simply not needed. 这是“ math.round”函数的一种奇怪行为,还是只是根本不需要它。 I have seen it a lot but when I look at it I feel it can be omitted, but I am not a computer... 我看过很多东西,但是当我看它的时候我觉得它可以省略,但是我不是电脑...

(60000/bpm) * 3 gives the same result ((60000/bpm) *3*1000)/1000 (60000 / bpm)* 3给出相同的结果((60000 / bpm)* 3 * 1000)/ 1000

If you look carefully you find that the whole term is divided by 1000 after rounding. 如果仔细看,会发现四舍五入后将整个项除以1000。

So it is not just x * 1000 / 1000 . 因此,不仅是x * 1000 / 1000

Math.round(a*1000)/1000 results in number a rounded with 3 decimals. Math.round(a*1000)/1000数字a舍入为3位小数。

Ex: Math.round(1234.123456 * 1000)/1000 = 1234.123 例如:Math.round(1234.123456 * 1000)/ 1000 = 1234.123

How this works is like this: Suppose the number a has x decimals (in our example 6). 它的工作方式如下:假设数字ax个小数(在我们的示例6中)。 You multiply the number by 10 to the power of n (in our example 3), effectively moving the decimal point n digits to the right. 将数字乘以10乘以n的幂(在我们的示例3中),有效地将小数点向右移动n位数字。 Then you round the number (cut all decimals). 然后,将数字四舍五入(切掉所有小数点)。 Then you divide by 10 to the power of n , moving the decimal point back. 然后,将10除以n的幂,然后将小数点移回原位。

It has to do with the parentheses. 它与括号有关。

Math.round((60000/bpm)*3*1000)/1000

In full it reads.. 全文阅读。

Divide 60000 by bpm then multiply by 3000 then perform Math.round then divide by 1000 将60000除以bpm然后乘以3000然后执行Math.round然后除以1000

You are rounding a possible float before dividing it by 1000 您正在对可能的浮点数进行四舍五入,然后除以1000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM