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?

有帮助吗?

解决方案

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];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top