Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript remove numbers after the dot

Lets say i have this number: 1.5676556 But i want it to show only 1 (without rounding, lets say i have 0.6, if i use math.round, it will round it to 1)

tnx in advanced.

like image 995
WEBProject Avatar asked Jul 04 '10 09:07

WEBProject


People also ask

How do I strip a number from a string in JavaScript?

To remove all numbers from a string, call the replace() method, passing it a regular expression that matches all numbers as the first parameter and an empty string as the second. The replace method will return a new string that doesn't contain any numbers.

How do you remove the last digit of a number in JavaScript?

substring()" is used to remove the last digit of a number.


3 Answers

Use Math.floor(0.6), or if you really want to use string manipulation, String(0.6).split('.')[0].

like image 148
nikc.org Avatar answered Oct 15 '22 06:10

nikc.org


Try this:

0.12345678.toFixed(2) // "0.12"
like image 43
Asaf Katz Avatar answered Oct 15 '22 08:10

Asaf Katz


You are looking for Math.floor if the number greater than 0.

like image 39
Emrah GOZCU Avatar answered Oct 15 '22 07:10

Emrah GOZCU