Java how to get remainder from calculation

Dear all,

How do I retrieve the remainder of the timestep_as_double calculation.

double timestep_as_double = 98/48;

i.e. the value of above calculation is 2.01466666, I want to pull out .0146666.

Thanks
Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
You're probably surprised that
double timestep_as_double = 98/48;

set timestep_as_double as 2.  This is because you're dividing two integers together - and Java isn't smart enough to know that you want a floating point value at the end.  So it does integer division and 98/48 rounded down as an integer is 2.

So you probably want this instead:

double timestep_as_double = 98.0/48.0;

and then you can get the integer part and remainder like this:

// This will be 2
int intPart = (int)timestep_as_double ;

// Remainder as a decimal (0.04...)
double remainder = timestep_as_double - intPart ;

// Remainder as a fraction of 48ths as in 2/48th
int remainderAsInt = (int)(Math.round(remainder * 48.0)) ;

Hope that helps,

Doug

Experts Exchange Solution brought to you by