Domanda

Is there a "simple" js version of php's bcdiv? Because this isn't working as expected:

var x = 11, y = Math.round(x / 7) + " + " + x % 7)

The expected result is 1 + 4, while the result i get is 2 + 4 (because 11 / 7 = 1,5 which becomes 2 after rounding).

Using Math.floor or Math.ceil also gives a wrong result.

What i try is to calculate the difference between two dates in weeks and days. Using bcdiv in php works fine, i just cant find a proper way in JS.

What i want: 16.04.2011 to 15.12.2013 = 139 Weeks, 1 Day

What i get: 16.04.2011 to 15.12.2013 = 137 Weeks, 1 Day

È stato utile?

Soluzione

Use parseInt instead of Math.round to truncate the decimal part, like this

var x = 11, y = parseInt(x / 7) + " + " + x % 7;
console.log(y)

Output

1 + 4
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top