Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is java making when round the number 6.02E23?

Tags:

java

I wanna know why do java print me that number 9223372036854775807 when I write that statement System.out.print(Math.round(6.02e23). What exactly is java doing?

like image 591
Ask8 Avatar asked Mar 04 '23 11:03

Ask8


1 Answers

Math.round() returns a long. But your number overflows Long's max value, which makes it return the maximum long value possible: 9223372036854775807

From the JavaDoc:

If the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE, the result is equal to the value of Long.MAX_VALUE.

6.02e23        = 602000000000000000000000 
Long.MAX_VALUE =      9223372036854775807
like image 200
Ayrton Avatar answered Mar 15 '23 20:03

Ayrton