0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[TypeScript] 小数点以下の桁数を指定して四捨五入する

Posted at
const roundWithScale = (value: number, scale: number) => {
  return Math.round(value * 10 ** scale) / 10 ** scale;
};

roundWithScale(1.456789, 0) // 1
roundWithScale(1.456789, 1) // 1.5
roundWithScale(1.456789, 2) // 1.46
roundWithScale(1.456789, 3) // 1.457
0
0
2

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?