I would like to know what would be the most elegant approach to extract digits from a double in ObjectiveC using Cocoa Touch (this needs to run on an iPhone):
Let's suppose you have a double : 1.423
How would you get each "1", "4", "2", "3", that compose the double in several variables ?
In the end I would like to get something like :
NSLog(@"here are the digits : %d , %d %d %d ", one, two, three, four);
one variable should be 1
two variable should should be 4
three variable should be 2
four variable should be 3
Any advice to achieve this in a nice way using ObjectiveC / cocoa Touch ?
Thanks.
Here is something I whipped up for you real quick.
Then in your code, do something like this:
I'd convert it to a string (using
+[NSString stringWithFormat:]
) and then scan out the numbers usingrangeOfCharactersInSet:
or anNSScanner
.