Vaagn Stepanian
Vaagn Stepanian

Reputation: 97

How get result from php the same on java?

Why on php this printf((2376995291 - 141 * 16777216) / 65535) on

result 174.0724040589.

On Java System.out.println((2376995291L - 141 * 16777216) / 65535)

result 65711 .

Why its two different result and how get result PHP on Java , i need get 174 on java.

Upvotes: 2

Views: 81

Answers (5)

Rajendra Singh
Rajendra Singh

Reputation: 464

In java Try this

System.out.println((2376995291L - 141 * 16777216L) / 65535L);

Upvotes: 1

khelwood
khelwood

Reputation: 59146

141 * 16777216 is too big for an int. If you do that part of the calculation as a long, you'll get the right answer.

jshell> (2376995291L - 141 * 16777216L) / 65535
==> 174

Upvotes: 0

Mak
Mak

Reputation: 1078

Because multiplication of is long number.Need to define it as long

Splited into 2 step for just clarification.

    public static void main(String[] args) {
            Long te = (141L * 16777216L);
            System.out.println((2376995291L - te) / 65535);

        }

Upvotes: 0

nice_dev
nice_dev

Reputation: 17805

System.out.println((2376995291L - 141L * 16777216L) / (double)65535);

141 * 16777216 will technically overflow since it's greater than 2^31 - 1. So, make them as long numbers to avoid overflow and you could typecast the denominator if you like to get the result with decimal points as well.

Upvotes: 4

devgianlu
devgianlu

Reputation: 1580

You need to cast the number to float or Java will simply ignore the decimal places.

System.out.println(((float) (2376995291L - 141L * 16777216L)) / 65535f)

Upvotes: 0

Related Questions