开发者

number too large

Hey , i hope i get help with this.

im a coder of a rsps (runescape private server)

and in this game you could like have items and weapons

and the max anmount of a item you can have is 2147000000

and i can change the amount of the max by changing this int

public int maxItemAmount = 2147000000;

and it works

but i want to make it like 3000000000

and i do this

public int maxItemAmount = 3000000000;

and when i compile i get this error

integrer number too large开发者_Python百科: 3000000000

please guys help me out if you can :)


Integer has an upper bound of 2^31 (2147483648). If you want numbers longer than that, you can use a long or double.


Integers are signed 32-bit values and thus can have a maximum value of 231 = (note that one bit is used for the sign).

You need to change the type of maxItemAmount to long.


you should go for 64 bit types ... i.e: long ...

do ...

public long maxItemAmount = 3000000000L;


A 32-bit signed integer has a range of -232 to 232-1, or − 2,147,483,648 to 2,147,483,647. If you want an integral value outside this range you need to use a 64-bit (long) variable. On the other hand, how likely is it that anyone will have over 2 billion distinct items or weapons? Perhaps, you want to rethink it and keep track of items and their quantities separately. You might also want to consider that changing to use a long may have unexpected consequences if parts of the code assume that it's a 32-bit value.


You can check out the max value of numbers with Integer.MAX_VALUE and Long.MAX_VALUE.

If a long is still not enough for your needs, you can check out the BigInteger class.


Instead of int you should use long. Here you can find more information about the precision of Java primitive data types.

Quoting from the link:

long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int.


As other people has sayed, you have to use another data type that support big numbers.
I recommend you using long. For example:

public long maxItemAmount = 3000000000L;

Notice the L at the end of the value. It tells the runtime that 3000000000 is a long value. Or use:

public long maxItemAmount = new Long("3000000000");

See more info on this page.


Also, someone commented using a BigInteger. It's like a String; don't have limit.
However, I recommend you to use it rarely. A long will probably suit your needs.

EDIT: Strictly speaking, BigIntegers and Strings have limit (see comments).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜