How to get the decimal part of a float?

前端 未结 10 784
清酒与你
清酒与你 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 10:47

    I bit long but works:

    BigDecimal.valueOf(2.65d).divideAndRemainder(BigDecimal.ONE)[1].floatValue()
    

提交回复
热议问题