-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

Albert
  • 1
  • 2
  • This sounds like an [X/Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You shouldn't be in the position of needing to convert `10` to `.00001` based on the perceived number of places-after-the-decimal in the first number, not least because numbers have no intrinsic places-after-the-decimal concept. One moment it could be 3, the next 7, depending on the value. What is the underlying X problem? – T.J. Crowder Mar 30 '21 at 10:06
  • Don't worry i need only to from whole number substract -10 but last two number. – Albert Mar 30 '21 at 10:09
  • Crowder I need to move the latitude -10 every next one. means to be below the pin. So if he is now 11.123456 the next one must be the last 2 decimal places by 10 less. @T.J.Crowder – Albert Mar 30 '21 at 10:42
  • You're missing the point. `40` is a valid latitude. So are `40.1`, `40.12`, `40.123`, etc. The `number` type has no concept of how many places there are after the decimal. `40` and `40.000000` are **exactly** the same thing. So anything where your starting point is figuring out how many digits there are to the right of the decimal point of a number is going to fail. Instead, pick your precision and use it, e.g. subtract `.00001` rather than `10`, `.000001` instead of `1`, etc. – T.J. Crowder Mar 30 '21 at 10:46
  • @T.J.Crowder Yes you are right. But how do I do that? – Albert Mar 30 '21 at 10:54
  • By doing that. Use `.0001` instead of `10`. Use `.000001` instead of `1`. If needed, with math (it's dividing by a million). Side note: `64.131577 - 76 = 64.131571` doesn't make any sense, it's closer to 64.131501. – T.J. Crowder Mar 30 '21 at 11:03

1 Answers1

1

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"
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875