let number = 64.129922
console.log(number - 10); //here i need only minus last 2
console.log(number - 10); //here i need only
le.log(number - 10); //here i need only minus last 2
let number = 64.129922
console.log(number - 10); //here i need only minus last 2
console.log(number - 10); //here i need only
le.log(number - 10); //here i need only minus last 2
You cannot reliably use the number of digits after the decimal in a number. The number
type doesn't have any concept of how many digits are after the decimal (and the algorithm to determine how many to include when creating a string for the number is complex.
If you always want to adjust the values you're subtracting to turn 10
into .00001
and 1
into .000001
, etc., then what you're doing is dividing them by 1,000,000:
function addAdjusted(base, addend) {
return base + (addend / 1000000);
}
console.log(addAdjusted(64.131577, -10)); // 64.131567 (roughly)
console.log(addAdjusted(64.131577, -1)); // 64.131576 (roughly)
console.log(addAdjusted(64.131577, -76)); // 64.131501 (roughly)
Note that at those scales, the number type becomes fairly imprecise. If you're going to output the results as strings, you might consider using toFixed(6)
to adjust the output:
function addAdjusted(base, addend) {
return base + (addend / 1000000);
}
console.log(addAdjusted(64.131577, -10).toFixed(6)); // "64.131567"
console.log(addAdjusted(64.131577, -1).toFixed(6)); // "64.131576"
console.log(addAdjusted(64.131577, -76).toFixed(6)); // "64.131501"