Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cdn/radash.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,9 @@ const toInt = (value, defaultValue) => {
const result = parseInt(value);
return isNaN(result) ? def : result;
};
function round(value, precision = 0) {
return Number(`${Math.round(Number(`${value}e+${precision}`))}e-${precision}`);
}

const shake = (obj, filter = (x) => x === void 0) => {
if (!obj)
Expand Down Expand Up @@ -937,4 +940,4 @@ const trim = (str, charsToTrim = " ") => {
return str.replace(regex, "");
};

export { all, alphabetical, assign, boil, callable, camel, capitalize, chain, clone, cluster, compose, construct, counting, crush, dash, debounce, defer, diff, draw, first, flat, fork, get, group, guard, inRange, intersects, invert, isArray, isDate, isEmpty, isEqual, isFloat, isFunction, isInt, isNumber, isObject, isPrimitive, isPromise, isString, isSymbol, iterate, keys, last, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, max, memo, merge, min, objectify, omit, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, select, series, set, shake, shift, shuffle, sift, sleep, snake, sort, sum, template, throttle, title, toFloat, toInt, toggle, trim, tryit as try, tryit, uid, unique, upperize, zip, zipToObject };
export { all, alphabetical, assign, boil, callable, camel, capitalize, chain, clone, cluster, compose, construct, counting, crush, dash, debounce, defer, diff, draw, first, flat, fork, get, group, guard, inRange, intersects, invert, isArray, isDate, isEmpty, isEqual, isFloat, isFunction, isInt, isNumber, isObject, isPrimitive, isPromise, isString, isSymbol, iterate, keys, last, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, max, memo, merge, min, objectify, omit, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, round, select, series, set, shake, shift, shuffle, sift, sleep, snake, sort, sum, template, throttle, title, toFloat, toInt, toggle, trim, tryit as try, tryit, uid, unique, upperize, zip, zipToObject };
4 changes: 4 additions & 0 deletions cdn/radash.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,9 @@ var radash = (function (exports) {
const result = parseInt(value);
return isNaN(result) ? def : result;
};
function round(value, precision = 0) {
return Number(`${Math.round(Number(`${value}e+${precision}`))}e-${precision}`);
}

const shake = (obj, filter = (x) => x === void 0) => {
if (!obj)
Expand Down Expand Up @@ -1009,6 +1012,7 @@ var radash = (function (exports) {
exports.replace = replace;
exports.replaceOrAppend = replaceOrAppend;
exports.retry = retry;
exports.round = round;
exports.select = select;
exports.series = series;
exports.set = set;
Expand Down
2 changes: 1 addition & 1 deletion cdn/radash.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export {
proxied,
throttle
} from './curry'
export { inRange, toFloat, toInt } from './number'
export { inRange, round, toFloat, toInt } from './number'
export {
assign,
clone,
Expand Down
9 changes: 9 additions & 0 deletions src/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,12 @@ export const toInt = <T extends number | null = number>(
const result = parseInt(value)
return isNaN(result) ? def : result
}

/**
* Rounds `value` to `precision` decimal places, mirroring lodash's `round`.
* Uses exponential notation internally to avoid the common floating-point
* issue (e.g. `round(1.005, 2)` is `1.01`, not `1`). See issue #61.
*/
export function round(value: number, precision = 0): number {
return Number(`${Math.round(Number(`${value}e+${precision}`))}e-${precision}`)
}
16 changes: 16 additions & 0 deletions src/tests/number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,19 @@ describe('number module', () => {
})
})
})

describe('round function', () => {
test('rounds to integer by default', () => {
assert.strictEqual(_.round(2.6), 3)
assert.strictEqual(_.round(2.4), 2)
})
test('rounds to given precision avoiding floating point issues', () => {
assert.strictEqual(_.round(1.005, 2), 1.01)
assert.strictEqual(_.round(1.2345, 2), 1.23)
assert.strictEqual(_.round(1.235, 2), 1.24)
})
test('handles zero value and negative numbers', () => {
assert.strictEqual(_.round(0, 2), 0)
assert.strictEqual(_.round(-1.5, 0), -1)
})
})
Loading