Domanda

I am creating a calculator app. For aesthetic reasons (numbers need to line up with an image), the result always needs to be five digits long, even if the first four digits are spaces or zeros. For example, the user enters 1+1, the result is 2, but my app should display it as "00002" or " 2".

How can I achieve this?

È stato utile?

Soluzione

Well, you contradict yourself when you say the number should always be five digits long then say your app should display it as "00002" or "2". I'll assume you actually want the former? Here's a simple example assuming your number is int x.

int x = 2;
NSString *string = [NSString stringWithFormat:@"%05d", x];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top