I need to convert two numbers to string in Objective-C.
One is a long number and the other is a float.
I searched on the internet for a solution and everyone uses stringWithFormat: but I can’t make it work.
I try
NSString *myString = [NSString stringWithFormat: @"%f", floatValue]
for 12345678.1234 and get “12345678.00000” as output
and
NSString *myString = [NSString stringWithFormat: @"%d", longValue]
Can somebody show me how to use stringWithFormat: correctly?
This article discusses how to use various formatting strings to convert numbers/objects into NSString instances:
String Programming Guide: Formatting String Objects
Which use the formats specified here:
String Programming Guide: String Format Specifiers
For your float, you’d want:
And for your long:
But honestly, it’s sometimes easier to just use the
NSNumberclass: