JavaScript How to round a decimal number to 2 decimal place?

preview_player
Показать описание
There is a random decimal number.
Let’s define round-function with two parameters.
The value to round and precision.
There already exists a Round function in the Math object.
But this function has no precision parameter.
So we need to multiply the value by the power of ten, then round it, and then divide it again by the same power of ten.
To exclude rounding errors we have to add the smallest decimal number to the value.
So let’s test it.
The initial value.
Rounded with three digits, two digits and one digit.

Thank you for watching to the end. Subscribe to our channel and you will learn a lot more interesting things.
Рекомендации по теме