LoginSignup
2
0

More than 5 years have passed since last update.

JavaScriptで整数を安全な範囲で扱う

Posted at

 Number.MAX_SAFE_INTEGER、Number.MIN_SAFE_INTEGERを使えば、精度が保証される範囲の整数値を取得できるのでそれを使います。

 以下は、30 seconds of codeより引用です。

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
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0