How to get the decimal part of a float?

前端 未结 10 806
清酒与你
清酒与你 2020-11-28 10:43

I need to extract the decimal part of a float number, but I get weird results:

float n = 22.65f;
// I want x = 0.65f, but...

x = n % 1; // x = 0.6499996

x          


        
10条回答
  •  伪装坚强ぢ
    2020-11-28 11:07

    The sound and perfect way to get decimal part of float and double data types, is using with String like this code:

    float num=2.35f;
    String s= new Float(num).toString();
    String p=s.substring(s.indexOf('.')+1,s.length());
    int decimal=Integer.parseInt(p);
    

提交回复
热议问题