Question

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?

Was it helpful?

Solution

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top