Skip to content

Latest commit

 

History

History
19 lines (15 loc) · 414 Bytes

toSafeInteger.md

File metadata and controls

19 lines (15 loc) · 414 Bytes
title tags
toSafeInteger
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.
const toSafeInteger = num =>
  Math.round(Math.max(Math.min(num, Number.MAX_SAFE_INTEGER), Number.MIN_SAFE_INTEGER));
toSafeInteger('3.2'); // 3
toSafeInteger(Infinity); // 9007199254740991