Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the quotient as int and remainder as a floating point in JavaScript

On my calculator when I do 18/7 I get 2.5714285714285714285714285714286. From my super limited Math skills 2 is the quotient and .5714285714285714285714285714286 is the remainder.

How can I model this in JavaScript?

Thanks!

like image 571
Sebastian Patten Avatar asked Sep 02 '25 14:09

Sebastian Patten


2 Answers

var floatingPointPart = (18/7) % 1;
var integerPart = Math.floor(18/7);
like image 172
Zéychin Avatar answered Sep 05 '25 03:09

Zéychin


Math.floor has the problem of rounding of the result to wrong direction in case of negative number it would be better to use bitwise operation to obtain interger quotients.

var quot = ~~(num/num1)

Hopefully this works for you!

like image 34
Jibin Mathew Avatar answered Sep 05 '25 04:09

Jibin Mathew