Frage

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?

War es hilfreich?

Lösung

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];
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top