0

I have some lines of code that produces a number after calculation which is currently in JavaScript and here is the code:

if ( y2 != y1 )
{
    // calculate rate
    var f;
    var y;
    if ( y2 > y1 )
    {
        f = cpi[y2] / cpi[y1];
        y = y2 - y1;
    }
    else
    {
        f = cpi[y1] / cpi[y2];
        y = y1 - y2;
    }           
    var r = Math.pow(f, 1/y);
    r = (r-1)*100;
    r = Math.round(r*100) / 100;
    System.out.println( "number: " + r.toFixed(2) + "%." );
}

I converted the above JS code to Java and here is the code:

DecimalFormat decimalFormat = new DecimalFormat("0.##");
    if (cpi[to] != cpi[from]) {
        double f, y;
        if (cpi[to] > cpi[from]) {
            f = cpi[to] / cpi [from];
            y = to - from;
        }
        else {
            f = cpi[from] / cpi[to];
            y = from - to;
        }
        q = Math.pow(f, 1/y);
        q = (q-1)*100;
        q = Math.round(q*100)/100;
        Toast.makeText(getApplicationContext(), "number: " + String.valueOf(decimalFormat.format(q)), 2000).show();
    }

The JavaScript code produces: number: 2.39

While the Java code produces number: 2

Why am I getting two different value? I will post what cpi[to], cpi[from], to and from values are if needed.

7
  • 2
    Is q declared as a double somewhere? Commented Oct 10, 2013 at 20:07
  • 1
    Please post the definition of cpi, maybe it's an int[], and integer division is truncating a quotient. Commented Oct 10, 2013 at 20:08
  • 1
    Try to change q = (q-1)*100.0; and q = Math.round(q*100.0)/100.0; Commented Oct 10, 2013 at 20:10
  • 1
    Maybe you should use 100.0 instead of 100: like q = Math.round(q*100) / 100.0 Commented Oct 10, 2013 at 20:10
  • 1
    Try replacing the 100s with 100.0? Not sure if that's the issue though to be honest... -- [oops, looks like it is the issue, and others beat me to it!] Commented Oct 10, 2013 at 20:12

4 Answers 4

2

In this line

q = Math.round(q*100)/100;

both operands of the division operation are integral, therefore the result is also an integral type. Use 100.0 as the divisor to coerce the result to a double.

Sign up to request clarification or add additional context in comments.

1 Comment

You are right. I cant accept the answer yet because of SO :/!
1

what is q type? try to put 100.0 also

Comments

1

If you devide two integers in java the result is integer. Every operation with double and integer or with two doubles creates double.

1)In this line

1)q = Math.round(q*100)/100;

2)you are deviding two integers, so it has same output as:

2)q = (int) (Math.round(q*100)/100);

3)you can use casting to double for example:

3)q = Math.round(q*100)/(double)100;

4)or using the 100.0 which makes this number double:

4)q = Math.round(q*100)/100.0;

5)this should work too, because first the result of Math.round is converted to double and then devided by 100:

5)q = (double)Math.round(q*100)/100;

6)However this WILL NOT work, because first Math.round is devided by 100 and it creates integer, so the result of this operation is rounded down and AFTER then casted to double. So it will be double but still rounded down, because it was rounded before it becomes double.

6)q = (double)(Math.round(q*100)/100);

2 Comments

What if I have currency and I want to force .## always. How can I do that? I will open a new question for that.
You have to have one of the variable double or casted to double. So for example : double result = a/(double)b
0

Most likely your issue is Math.round(double) returns a long value. So d would contain a long instead of a double at that point.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.