Why calculation in android with in php different?
Asked Answered
B

1

6

I have problem in calculation in android, i am doing the calculation

((1 * (1 + ((0.025 * 12) * ((6-1) / 12))) / 6);

In php generate value

echo ((1*(1+((0.025*12)*((6-1)/12))))/6);

Result :

0.1875

But in android :

System.out.println(((1*(1+((0.025*12)*((6-1)/12))))/6));

Result :

0.16666666666666666

I've tried everything to do rounding on decimal values, but the results are different. How do i do the exact calculation to be able to adjust to the given value of php?

Thank you in advance.

Backset answered 14/7, 2017 at 11:11 Comment(0)
G
6

You need to cast the return value of all decimal numbers to double or float, to get all the decimals. It'll default to integers, which mean you loose some data.

double d = (((1 * (1 + ((0.025 * 12) * ((double)(6 - 1) / 12)))) / 6));
Gives 1.875

double f = (((1 * (1 + ((0.025 * 12) * ((6 - 1) / 12)))) / 6));
Gives 1.666666666

Gunilla answered 14/7, 2017 at 11:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.