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.

JavaScriptで小数点以下切り捨てを簡単に書く

Posted at

Twitterで回ってきたJSでの小数点以下切り捨ての方法があったので試しに自分でも書いてみた。
書いてみた故に、記念でこちらにも書いてみた。
これはアドベントカレンダーをnoteに書いた贖罪でもある(笑)

小数点以下切り捨て

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>eliminate decimal</title>
    </head>
    <body>
        <h1>小数点以下を切り捨てする方法</h1>
        <script>
            let num = Math.random() * 100;
            console.log(num); //45.602820325879925

            //floorで小数点以下切り捨てと同じ処理
            num = ~~num;
            //num = Math.floor(num); これと同じ処理

            console.log(num); //45
        </script>
        <footer><small>G's</small></footer>
    </body>
</html>


0
0
3

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?