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

前端 未结 5 1281
梦毁少年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:11

    You'll have to do it when parsing the string. If there's a decimal point in the string, treat it as percentage, otherwise it's just the integer value.

    So, e.g.:

    rgb 1 1 1   // same as #010101
    rgb 1.0 1 1 // same as #ff0101
    

    Since the rgb is there, you're parsing the string anyway. Just look for . in there as you're doing it.

提交回复
热议问题