Organizational Research By

Surprising Reserch Topic

how to get the decimal part of a float using -'java,floating-point'


how to get the decimal part of a float  using -'java,floating-point'

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 = n - Math.floor(n); // x = 0.6499996185302734

x = n - (int)n; // x = 0.6499996


Why does this happen? Why do I get those values instead of 0.65?
    
asked Sep 7, 2015 by rajesh
0 votes
3 views



Related Hot Questions



Government Jobs Opening


...