WEBProject
WEBProject

Reputation: 1335

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.

Upvotes: 10

Views: 22222

Answers (4)

LuckyLuke Skywalker
LuckyLuke Skywalker

Reputation: 791

Use Math.trunc(7.814);

Do not use Math.floor or similar since they round the number instead of just removing the digits after the comma/dot. I do not know how this has been back in the day but because of this the accepted answer is false nowadays.

Upvotes: 0

Asaf Katz
Asaf Katz

Reputation: 4688

Try this:

0.12345678.toFixed(2) // "0.12"

Upvotes: 15

Emrah GOZCU
Emrah GOZCU

Reputation: 336

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

Upvotes: 1

nikc.org
nikc.org

Reputation: 16952

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

Upvotes: 19

Related Questions