Javascript casts floating point numbers to integers without cause

后端 未结 2 1294
攒了一身酷
攒了一身酷 2021-01-21 07:41

I wrote a function that behaves differently depending on the numeric type of it\'s parameters. Integer or float.

Using some code from this question How do I check that a

相关标签:
2条回答
  • 2021-01-21 08:20

    You've acknowledged that JavaScript only has a single Number type. As such, 1 is identical to 1.0.

    If you need this for display purposes, then you should use toFixed.

    1..toFixed(1); // "1.0"
    
    0 讨论(0)
  • 2021-01-21 08:22
    number%1===0 
    

    If that condition is true , it's integer, else it's float

    0 讨论(0)
提交回复
热议问题