I have a UILabel in my iPhone app simulator. It displays a coin count and I have an action that adds 1 hundred million to the count. I want the number to keep going up but for some reason once the count hits 2 billion, it adds a minus sign and starts counting down, then counts back up to 2 billion and back down again and so on.
I want to be able to display a much greater number of digits ie trillions and so on… Does anyone know what’s going on with this and how to fix it so the label digits will keep going up as high as I want.
I’m using Xcode and Interface Builder and running through the simulator. I’m storing the number in a int variable, if that matters.
You store your coin count in an
int, that’s the problem. A 4 byteintcan’t store numbers higher than 2,147,483,647. If you add 1 to 2,147,483,647 you will get −2,147,483,648, which is the smallest possibleint.If you want to store bigger numbers you have to use a
longwhich can store numbers between −(2^63) and 2^63−1 (or −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807).See this for additional details.