[Solved] Math.ceil not working with negative floats


The Math.ceil method does actually round up even for negative values. The value -12 is the closest integer value that is at higher than -12.369754.

What you are looking for is to round away from zero:

value = value >= 0 ? Math.ceil(value) : Math.floor(value);

Edit:

To use that with different number of decimal points:

// it seems that the value is actually a string
// judging from the parseFloat calls that you have
var value="-12.369754";
var decimalPoints = 0;

// parse it once
value = parseFloat(value);

// calculate multiplier
var m = Math.pow(10, decimalPoints);

// round the value    
value = (value >= 0 ? Math.ceil(value * m) : Math.floor(value * m)) / m;

console.log(value);

Demo: http://jsfiddle.net/Guffa/n7ecyr7h/3/

2

solved Math.ceil not working with negative floats