JavaScript - How To Detect Number As A Decimal (Including 1.0)

前端 未结 5 1288
梦毁少年i
梦毁少年i 2021-02-10 15:41

It feels like I am missing something obvious here. This has been asked a number of times - and the answer usually boils down to:

var num = 4.5;
num % 1 === 0; /         


        
5条回答
  •  粉色の甜心
    2021-02-10 16:10

    var num = 1;
    

    and

    var num = 1.0;
    

    are the same. You mention that you want to treat them differently when given from a user. You will want to parse the difference when it is still a string and convert it to the appropriate number.

提交回复
热议问题