coder-guy-2020
coder-guy-2020

Reputation: 237

how to round to 2 decimal places with typescript?

I need to write a function in TypeScript which can round a number to 2 decimal places. For example:

123.456 => should return 123.46

123.3210 => should return 123.32

Can you identify the TypeScript function that I can use to accomplish this? Alternatively, is there is a commonly used third party library used for this, then what library and function should I use?

Upvotes: 21

Views: 36058

Answers (5)

MatDev
MatDev

Reputation: 1

I have this solution for invoice application, it allow to round positive number (0.005 ==> 0.01) and négative number for credit (-0.005 ==> 0.01)

round(num: number, fractionDigits: number = 2): number {
        if (!isFinite(num)) return num; // Handle NaN, Infinity, and -Infinity

        const factor = Math.pow(10, fractionDigits);
        const result = Math.round((num + Math.sign(num) * Number.EPSILON) * factor) / factor;

        return result === 0 ? 0 : result; // Ensure `-0` becomes `0`
    }

I think something like this could be the solution at the probleme for positive number (0.005=>0.01) and négative number (-0.005 ==> 0):

round(num: number, fractionDigits: number = 2): number {
if (!isFinite(num)) return num; // Handle NaN, Infinity, and -Infinity
if (isNaN(num)) return NaN;

const factor = Math.pow(10, fractionDigits);
return result === 0 ? 0 : Math.round(num * factor) / factor;

}

Upvotes: 0

zemil
zemil

Reputation: 5090

For the easiest-to-remember solution but sometimes it can be invalid preceding:

function round(num: number): number {
  return Math.round(num * 100) / 100;
}

For more specific and to ensure things like 1.005 round correctly:

function round(value: number, decimals = 2): number {
    return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);
}

Upvotes: 0

Razze
Razze

Reputation: 4302

You can use toFixed, it rounds but it returns a string. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

So your function body will most likely end up with something like

function round(num: number, fractionDigits: number): number {
    return Number(num.toFixed(fractionDigits));
}

Upvotes: 25

Boppity Bop
Boppity Bop

Reputation: 10481

Idk why everything in web-world turns to circus but it seems there is no out-of-the-box solution. I found this blog and it works as far as i could test it:https://expertcodeblog.wordpress.com/2018/02/12/typescript-javascript-round-number-by-decimal-pecision/

  public round(number: number, precision: number) {
    if (precision < 0) {
      let factor = Math.pow(10, precision);
      return Math.round(number * factor) / factor;
    }
    else
      return +(Math.round(Number(number + "e+" + precision)) +
        "e-" + precision);
  }
}

Upvotes: 4

Chamika
Chamika

Reputation: 167

I have used following code to round to 2 decimal places

(Math.round(val * 100) / 100).toFixed(2);

Upvotes: 8

Related Questions