Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to round down number 2 decimal places? [duplicate]

Tags:

javascript

How to get following inputs to bellow outputs

Input

1.0789 10.350 1.7777 

Output

1.07 10.35 1.77 
like image 926
I don't know Avatar asked Dec 21 '16 09:12

I don't know


People also ask

How do you truncate a double to two decimal places?

Shift the decimal of the given value to the given decimal point by multiplying 10^n. Take the floor of the number and divide the number by 10^n. The final value is the truncated value.

How do you simplify 2 decimal places?

Rounding a decimal number to two decimal places is the same as rounding it to the hundredths place, which is the second place to the right of the decimal point. For example, 2.83620364 can be round to two decimal places as 2.84, and 0.7035 can be round to two decimal places as 0.70.


1 Answers

Use Math.floor to round the decimal places under the current value.

Reference

Example

Math.floor(1.0789 * 100) / 100 

Working Fiddle

console.log(Math.floor(1.0789 * 100) / 100); console.log(Math.floor(10.350 * 100) / 100); console.log(Math.floor(1.7777 * 100) / 100); console.log(Math.floor(12.34 * 100) / 100);
like image 118
Nitheesh Avatar answered Sep 21 '22 23:09

Nitheesh