Files
30-seconds-of-code/snippets/toSafeInteger.md
2020-09-15 21:52:00 +03:00

20 lines
414 B
Markdown

---
title: toSafeInteger
tags: math,beginner
---
Converts a value to a safe integer.
- Use `Math.max()` and `Math.min()` to find the closest safe value.
- Use `Math.round()` to convert to an integer.
```js
const toSafeInteger = num =>
Math.round(Math.max(Math.min(num, Number.MAX_SAFE_INTEGER), Number.MIN_SAFE_INTEGER));
```
```js
toSafeInteger('3.2'); // 3
toSafeInteger(Infinity); // 9007199254740991
```