How to convert a floating-point number to an integer in JavaScript?
Hello everyone! I'm new to JavaScript and ran into a problem. I need to work with numbers, but often after mathematical operations I get floating-point numbers (e.g., 3.14 or 7.999), while I need integers. I tried using parseInt(), but it doesn't always work as I expect.
Here's an example of my code:
```javascript
let price = 19.99;
let quantity = 3;
let total = price * quantity; // get 59.97
// I need to round to an integer
console.log(parseInt(total)); // outputs 59
// But I wanted to get 60, since 59.97 is closer to 60
let negative = -3.7;
console.log(parseInt(negative)); // outputs -3, though I expected -4
```
I also noticed that Math.floor() and Math.ceil() behave differently with negative numbers. Please advise which method is correct for different
No answers yet.
Log in to leave an answer