Quantcast
Channel: Nifty Snippets
Viewing all articles
Browse latest Browse all 35

Well, I'm floored

$
0
0
How do you floor (or truncate) a floating-point number in JavaScript? (E.g., take a value like 5.7 and get just the 5?). The answer is simple: Use Math.floor. That's the right answer at least 99.99% of the time. It's clear, straightforward, easy to read, easy to maintain. It does what it says on the tin: console.log(Math.floor(5.7)); // "5" Sorted. But you see people doing other things in the

Viewing all articles
Browse latest Browse all 35

Trending Articles