Fix rounding issue with the precision param of _.floor.

This commit is contained in:
Tejo Kumar Reddy Chenchu
2015-09-24 01:12:13 -07:00
committed by John-David Dalton
parent 3fd276f8a6
commit dd27a0adc7
2 changed files with 37 additions and 12 deletions

View File

@@ -4081,8 +4081,15 @@
return function(number, precision) {
precision = precision ? toInteger(precision) : 0;
if (precision) {
precision = pow(10, precision);
return func(number * precision) / precision;
// Shift the decimal point with exponential notation to avoid floating-point funny bussiness.
// See [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round#Examples)
// for more details.
var pair = (+number + 'e').split('e'),
value = func(pair[0] + 'e' + (+pair[1] + precision));
// Shift back.
pair = (value + 'e').split('e');
return +(pair[0] + 'e' + (pair[1] - precision));
}
return func(number);
};