Check whether a float number contains decimals or not

前端 未结 7 2428
难免孤独
难免孤独 2020-12-29 07:02

How can I check whether a float number contains decimals like 2.10, 2.45, 12382.66 and not 2.00 , 12382.00. I want to know if the number is \"round\" or not. How can I do th

7条回答
  •  一生所求
    2020-12-29 07:52

    You could do this:

      float num = 23.345f;
      int intpart = (int)num;
      float decpart = num - intpart;
      if(decpart == 0.0f)
      {
        //Contains no decimals
      }
      else
      {
         //Number contains decimals
      }
    

提交回复
热议问题