Here is my 1-line solution: Number((yourNumericValueHere).toFixed(2));
Here's what happens:
1) First, you apply .toFixed(2)
onto the number that you want to round off the decimal places of. Note that this will convert the value to a string from number. So if you are using Typescript, it will throw an error like this:
"Type 'string' is not assignable to type 'number'"
2) To get back the numeric value or to convert the string to numeric value, simply apply the Number()
function on that so-called 'string' value.
For clarification, look at the example below:
EXAMPLE: I have an amount that has upto 5 digits in the decimal places and I would like to shorten it to upto 2 decimal places. I do it like so:
var price = 0.26453;_x000D_
var priceRounded = Number((price).toFixed(2));_x000D_
console.log('Original Price: ' + price);_x000D_
console.log('Price Rounded: ' + priceRounded);
_x000D_