rabbit-ear
Version:
origami design library
18 lines (15 loc) • 581 B
JavaScript
/* Rabbit Ear 0.9.4 alpha 2024-04-20 (c) Kraft, GNU GPLv3 License */
const countPlaces = function (num) {
const m = (`${num}`).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
return Math.max(0, (m[1] ? m[1].length : 0) - (m[2] ? +m[2] : 0));
};
const cleanNumber = function (number, places = 15) {
const num = typeof number === "number" ? number : parseFloat(number);
if (Number.isNaN(num)) { return number; }
const crop = parseFloat(num.toFixed(places));
if (countPlaces(crop) === Math.min(places, countPlaces(num))) {
return num;
}
return crop;
};
export { cleanNumber };