korvinko
korvinko

Reputation: 700

How to convert the last 3 digits of the number?

How to convert the last 3 digits of the number? Numbers will be bigger then 8000.

For example:

From 249439 to 249000?

Upvotes: 0

Views: 2487

Answers (4)

David Thomas
David Thomas

Reputation: 253358

I'd suggest the following:

function roundLastNDigits (num, digits) {
    // making sure the variables exist, and are numbers; if *not* we quit at this point:
    if (!num || !parseInt(num,10) || !digits || !parseInt(digits,10)) {
        return false;
    }
    else {
        /* otherwise we:
           - divide the number by 10 raised to the number of digits
             (to shift divide the number so that those digits follow
             the decimal point), then
           - we round that number, then
           - multiply by ten raised to the number of digits (to
             recreate the same 'size' number/restoring the decimal fraction
             to an integer 'portion' */
        return Math.round(num / Math.pow(10, parseInt(digits,10))) * Math.pow(10,digits);
    }
}

console.log(roundLastNDigits(249439, 3))

JS Fiddle demo.

If you'd prefer to always round down, I'd amend the above to give:

function roundLastNDigits (num, digits) {
    if (!num || !parseInt(num,10) || !digits || !parseInt(digits,10)) {
        return false;
    }
    else {
        return Math.floor(num / Math.pow(10, parseInt(digits,10))) * Math.pow(10,digits);
    }
}

console.log(roundLastNDigits(8501, 3))

JS Fiddle demo.

Simplifying the above by incorporating ruakh's genius approach:

function roundLastNDigits (num, digits) {
    if (!num || !parseInt(num,10) || !digits || !parseInt(digits,10)) {
        return false;
    }
    else {
        return num - (num % Math.pow(10,parseInt(digits,10)));
    }
}

console.log(roundLastNDigits(8501, 3))

JS Fiddle demo.

Or, finally, given that you only need to replace the last three digit characters with 0:

function roundLastNDigits (num, digits) {
    if (!num || !digits || !parseInt(digits,10)) {
        return false;
    }
    else {
        var reg = new RegExp('\\d{' + digits + '}$');
        return num.toString().replace(reg, function (a) {
            return new Array(parseInt(digits,10) + 1).join(0);
        });
    }
}

console.log(roundLastNDigits(8501, 3))

JS Fiddle demo.

References:

Upvotes: 1

progrenhard
progrenhard

Reputation: 2363

1) Math.round(num.toPrecision(3));

This doesn't account for the values before the 3rd value to round.

2)

This is sort of a bad solution but it works. num = 50343 // whatever your input is.

m = 10^n.

Math.round(num*m)/m

n being the amount you want to move over.

Upvotes: 0

ruakh
ruakh

Reputation: 183446

You can get the last three digits using the modulus operator %, which (for positive numbers) computes the remainder after integer division; for example, 249439 % 1000 is 439.

So to round down to the nearest thousand, you can just subtract those three digits:

var rounded = original - original % 1000;

(for example, if original is 249439, then rounded will be 249000).

Upvotes: 8

Paul S.
Paul S.

Reputation: 66354

For always rounding down I'd suggest dividing out 1000, casting to Int then multipling back in 1000

var x = 249439,
    y = ((x / 1000) | 0) * 1000; // 249000

Upvotes: 0

Related Questions