you dont know what what means? casting? start from the beginning of Tom Gunns code snippet:
'd' is a double (floating point) variable containing 23.4.
'w' is a double variable set to the value of 'd' once 'd' is casted as an integer. (see where 'd' is casted as an integer by the use of 'int' type in parentheses.) now 'w' contains the whole value 23.0 with no fractional part.
'p' is another double variable that is the value of the difference between 'd' and 'w'... since 'd' is 23.4 and 'w' is 23.0, the difference is 0.4, and this fractional part is assigned to 'p'
now 'd' still contains the original decimal value (23.4), and 'w' contains the whole number (23) and 'p' contains the fractional part (0.4)
.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
Floating type does not have fixed precision.
and if you enter
123.456
try to print that variable - you'll get something like that
123.455969706597869578956
So you have to convert its number into string and parse it.
Or choose precision and your code will be like that
int prec = 5;
int nf = (int)( ( n - nw ) * pow( 10, prec ) );
But this method is not precise.
Protuberance
Junior Poster in Training
90 posts since Aug 2009
Reputation Points: 78
Solved Threads: 17