mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 18:07:49 +00:00
Fix rounding issue with the precision param of _.floor.
This commit is contained in:
committed by
John-David Dalton
parent
3fd276f8a6
commit
dd27a0adc7
11
lodash.js
11
lodash.js
@@ -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);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user