Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why calculation in android with in php different?

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.

like image 496
Coranes Syahid Avatar asked Jul 14 '17 11:07

Coranes Syahid


1 Answers

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

like image 158
Moonbloom Avatar answered Sep 23 '22 20:09

Moonbloom