-1

So I was working on a fitness app that I have been on for quite some time. We were getting this weird defect where calories weren't increasing but distance was.. so I investigated. I started de-bugging, and saw duration was coming in as whole numbers, 0, 5.0, 2.0, etc. Yet this value was a double. The code below is part of a module, that we import as an .aar dependency via maven.

Here is the actual source code:

double duration = (System.currentTimeMillis() - mPreviousTimestamp) / 1000;

The decompiled class file, which I was de-bugging from:

double duration = (double)((System.currentTimeMillis() - this.mPreviousTimestamp) / 1000L);

Any thoughts as to, why this is happening, and a possible solution? Has anyone else seen this?

Ryan Newsom
  • 696
  • 7
  • 12

2 Answers2

2

The result of

(System.currentTimeMillis() - mPreviousTimestamp) / 1000

is an int. You have an int divided by an int. The result of that is an int. You then cast that int to a double via the assignment, but at that point the precision is gone.

You should do this,

double duration = (System.currentTimeMillis() - mPreviousTimestamp) / 1000.0;

By including the double (1000.0) in the calculation, it makes the result of the expression a double also.

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
0

Try:

Text.setText( String.format( "Value of a: %.8f", a ) );

For 8 decimal places!

King Dedede
  • 970
  • 1
  • 12
  • 28