chore: Added a function to truncate a number to ten thousandths (.000) without rounding off
This commit is contained in:
parent
4590bc8458
commit
d3027506a6
1 changed files with 14 additions and 0 deletions
14
utils/truncate_number.js
Normal file
14
utils/truncate_number.js
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
function truncate_number(value, decimals) {
|
||||||
|
const valueStr = value.toString();
|
||||||
|
const dotIndex = valueStr.indexOf('.');
|
||||||
|
if (dotIndex === -1) return valueStr;
|
||||||
|
const desiredLength = dotIndex + decimals + 1;
|
||||||
|
let truncated = valueStr.slice(0, desiredLength);
|
||||||
|
|
||||||
|
if (parseFloat(truncated) === 0 && value > 0) {
|
||||||
|
return valueStr;
|
||||||
|
}
|
||||||
|
return truncated;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { truncate_number };
|
Loading…
Add table
Reference in a new issue