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.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

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,


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.