Domanda

I am making a program and i need a way to make variables go over 10 billion and int only stores up to 999 million for me so i decided to use a long instead of a int and it turn out it only stores up to 999 million as well.

int TotalWorldPop = 7200000000; 

gives me the "literal is out of range" error

long TotalWorldPop = 7200000000;  

gives me the "literal is out of range" error as well

but

int TotalWorldPop = 999999999

is ok for me

È stato utile?

Soluzione

A long can accommodate numbers as large as 263-1. But there's a trick to putting them into the primitive field.

If you're entering the primitive literal, then you have to add an L at the end, as all numeric literals are treated as int (and it can only go up to ~2.1 billion).

If you need numbers larger than that, use BigInteger.

Altri suggerimenti

You could use a BigInteger to store very large numbers.

Example:

Biginteger bigInt1 = new Biginteger("91826581752671985235272769716");
Biginteger bigInt2 = new Biginteger("-1796357891266373473772242");
Biginteger bigint3 = bigInt1.divide(bigInt2);
Biginteger bigint4 = bigInt1.add(bigInt2);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top