Javascript casts floating point numbers to integers without cause

后端 未结 2 1295
攒了一身酷
攒了一身酷 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"
    

提交回复
热议问题