- How to divide the number in javascript completely?
- Is there any operator to divide completely?
- oneno way, you can only take a whole from the result - Grundy
- I understand, can I see a detailed answer with code examples, like stackowerflow in English? - Max
- A special case How to divide the number entirely - is specified by a javascript label, so it was not necessary to add another mention directly to the title. Discussion on a site - Grundy
- @Grundy label "javascript" does not solve the problem with unique headers, I added my answer on the topic to which you gave the link above. - Max
5 answers
There are several ways to divide by the number in JS.
Method 1. Rounding:
var x = 10, y = 3.3333; alert(Math.floor(x/y)); This method is calculated on the result of the calculation is greater than zero. If the result is negative, then this construction will not work correctly.
For example :
Math.floor(-100/3); // Выдаст -34, хотя целая часть от -33,33333336 будет равна -33 Alternatively, to solve this problem by rounding, you can use the if statement:
if(x/y>=0) alert(Math.floor(x/y)); else alert(Math.ceil(x/y)); Method 2: Probably not as fast as the previous one, but more versatile. Cast to int:
var x = 10, y = 3.3333; alert(parseInt(x/y)); Method 3 . Productive and versatile:
var x = 10, y = 3.3333; function div(val, by){ return (val - val % by) / by; } alert(div(x, y)); Well, a little Hadkor:
alert(~~(x/y)) // сокращенный Math.floor() результаты будут такие же alert(x/y>>0) alert(x/y|0) jsfiddle demo
In javascript, there is no division into integers and floating point numbers.
It is possible, as a consequence, that there are no special arithmetic operators for integers.
Based on this, there are several possible solutions:
Performing the usual division and taking from the result of the whole part. For this procedure, there are functions Math.floor and Math.ceil , the difference is that a larger integer or a smaller one will be selected.
console.log(Math.floor(10 / 3)); console.log(Math.floor(-10 / 3)); console.log(Math.ceil(10 / 3)); console.log(Math.ceil(-10 / 3));Apparently from an example, for positive numbers the
floorapproaches, for negativeceilThe use of bit operations. At the specification level, it is indicated that bit operations work only with 32-bit integers, so when working with them it is worth being careful: when applying them to large numbers, the upper bits of the number will be truncated . At the same time, this allows you to quickly take the integer part, by reducing the argument to an integer before performing a bit operation.
A widespread technique is the use of bitwise or with0, which leaves all the bits of the original number unchanged. And also a bitwise shift, also at 0 digits.console.log((10 / 3) | 0); console.log((-10 / 3) | 0); console.log((10 / 3) >> 0); console.log((-10 / 3) >> 0); console.log(' Неожиданно: ', (10000000000 / 2) | 0) console.log(' Неожиданно: ', (10000000000 / 2) >> 0)
As a variant of division, use this construction
function divme(a, b){ return (a - a%b)/b }not
UPD: I met another version of the division. It may be useful to you. Link
Here's another option:
Math.floor(a / b); And this one, but it’s better to be more careful with it - it works only for small numbers (somewhere up to 4e9):
a / b | 0 a / b | 0a / b | 0interesting post, how does it work? Perhaps it is better to make a separate question. - Max- 2As I understand it, to perform bitwise operations, the browser brings the number to an int (integer), while the “| 0” operation itself will not change a single bit. We can say the analogue of ((int) doubleValue) in other languages. - Surfin Bird
- 2@Max, with this operation you should be careful, as with any bit operations, the input argument is truncated to 32 bits, so the results can be unexpected - Grundy
- I see, thanks for clarifying - Max