vinograd
vinograd

Reputation: 93

how to change the last 5 digits in a number?

I don’t know how it is possible for the number to have the last 5 digits ... I will be very grateful for the help!

const num = 1666297292886;
//result 
1666297200000

Upvotes: 1

Views: 116

Answers (3)

Pac0
Pac0

Reputation: 23174

Adapt the 100000 below with the same number of zeros you want.

Remove the last 5 zeros: remainder of num divided by 100,000 is exactly the last 5 digits.

const num = 1666297292886;

const roundedDownResult = num - (num % 100000);
console.log(roundedDownResult);

In general, divide by the power of ten with the number of zeros you want at the end:

const num = 1666297292886;

for (let i = 0; i < 10; i++) {
    var precisionFactor = Math.pow(10, i);
    console.log(num - (num % precisionFactor ));
}

If you want the opposite (the last 5 digits), use the following.

const num = 1666297292886;

const lastDigits = num - (Math.floor(num / 100000) * 100000);
console.log(lastDigits);

Upvotes: 6

Brother58697
Brother58697

Reputation: 3178

You can also achieve this with strings:

const num = 1666297292886;

const output = num
   .toString() // or, (num + '') - Convert to string
   .slice(0,-5) //  - Remove last five characters
   .concat('0'.repeat(5)) // or concat('00000') - Append 5 zeroes
   - 0 // Convert string back to int
   
console.log(output)

Upvotes: 0

tenshi
tenshi

Reputation: 26332

This looks like flooring to the nearest "x", so let's do just that:

const x = 100000; // nearest 100,000

const input = 1666297292886;

const result = Math.floor(1666297292886 / x) * x;

console.log(result);

Upvotes: 1

Related Questions