ennvlxkm
2 months ago  Karma: 713
How to truncate the decimal part of a number in JavaScript?

How to truncate the decimal part of a number in JavaScript?

en
#javascript
ennvlxkm
2 months ago  Karma: 713
How to truncate the decimal part of a number in JavaScript?

How to truncate the decimal part of a number in JavaScript?

en
#javascript

2 ANSWERS
clam0
2 months ago Karma: 3307

float y = 3.14159f;

float x = Math.trunc(y);

sets x to 3.

you could also do

float y = 3.14159f;

int x = (int) Math.trunc(y);

sets x to 3.

float y = 3.14159f;

float x = Math.trunc(y);

sets x to 3.

you could also do

float y = 3.14159f;

int x = (int) Math.trunc(y);

sets x to 3.

brandon.pearson1011
2 months ago Karma: 1280

Yes, Math.floor works but an even faster and simpler alternative is to use the double bitwise not operator:
~~3.14159; //3

Yes, Math.floor works but an even faster and simpler alternative is to use the double bitwise not operator:
~~3.14159; //3

Earn tokens by posting and answering questions about blockchain!
Karma to eth
YOUR ANSWER