Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > int, double, float to stringValue??

int, double, float to stringValue??
Thread Tools
Senior User
Join Date: Jun 2002
Location: UK
Status: Offline
Reply With Quote
Sep 3, 2002, 08:00 AM
 
Why can't I get a string representation of a number?

I thought [myNumber stringValue] would work but it doesn't. So, what should I do. I've been surfing the web for 2 hours without a solution.

In other words, myNumber = 10 and I wast myString = myNumber = 10 [for a NSTextField and so on].
     
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Sep 3, 2002, 08:19 AM
 
If myNumber is an integer, do this:
Code:
NSString *numberAsString = [NSString stringWithFormat:@"%d", myNumber];
If it's a float, replace "%d" with "%f". Type man printf in Terminal to see a full list of tokens.

You say, however, that you want to use myNumber in an NSTextField, in which case you can just use -[NSTextField setIntValue:].
     
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Sep 3, 2002, 08:22 AM
 
Originally posted by VEGAN:
I thought [myNumber stringValue] would work but it doesn't.
A fundamental rule of Obj-C programming: you can only send messages to objects. -stringValue is a message, which you're trying to send to a basic C type. Only objects, such as instances of NSNumber are valid receivers of messages.
     
VEGAN  (op)
Senior User
Join Date: Jun 2002
Location: UK
Status: Offline
Reply With Quote
Sep 3, 2002, 08:29 AM
 
Thanks again! It worked.

I never thought I would have to use stringWithFormat!!!! This is a bit complicated... it might be OK for FLOAT values but for INT? Hmm..
     
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Sep 3, 2002, 08:35 AM
 
Originally posted by VEGAN:
This is a bit complicated... it might be OK for FLOAT values but for INT? Hmm..
How so? In a format string:
%@ = Objective-C object (like NSString)
%d = integer
%u = unsigned integer
%f = float
%0.1f = float rounded to 1 decimal place
%g = nicest (IMHO) for floating-point numbers; only displays as many decimal places as necessary
%s = C-string (char *)
%c = C-char
%% = a per cent (%) sign
This is what I remember of the top of my head. It's pretty easy really.
     
Senior User
Join Date: Nov 2000
Status: Offline
Reply With Quote
Sep 3, 2002, 02:10 PM
 
Code:
{ NSNumber *temp; int i = 1234; float f = 0.1234; temp = [[NSNumber alloc] initWithInt: i ]; // make use of this... // [temp stringValue] [temp release]; temp = nil; temp = [[NSNumber alloc] initWithFloat: f ]; // make use of this... // [temp stringValue] [temp release]; temp = nil; }
Is that easier than using NSString's -stringWithFormat:? I'd go ahead and check the speed of using -stringWithFormat vs. NSNumber. You may find either way acceptable but one will be faster than the other.

// typos fixed
     
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Sep 7, 2002, 02:49 AM
 
Originally posted by IamBob:

Is that easier than using NSString's -stringWithFormat:? I'd go ahead and check the speed of using -stringWithFormat vs. NSNumber. You may find either way acceptable but one will be faster than the other.
I did a little test as follows:
Code:
#import <Foundation/Foundation.h> int main(void) { NSAutoreleasePool *pool; int i; float time; pool = [[NSAutoreleasePool alloc] init]; time = 0.0; for (i = 0; i < 100000; i++) { NSDate *then = [NSDate date]; [[NSNumber numberWithInt:1234] stringValue]; time += [[NSDate date] timeIntervalSinceDate:then]; } NSLog(@"NSNumber Average Time: %f", time / 100000); [pool release]; pool = [[NSAutoreleasePool alloc] init]; time = 0.0; for (i = 0; i < 100000; i++) { NSDate *then = [NSDate date]; [NSString stringWithFormat:@"%d", 1234]; time += [[NSDate date] timeIntervalSinceDate:then]; } NSLog(@"NSString Average Time: %f", time / 100000); [pool release]; return 0; }
and the results were:

NSNumber Average Time: 0.000021
NSString Average Time: 0.000017

NSNumber Average Time: 0.000022
NSString Average Time: 0.000017

NSNumber Average Time: 0.000022
NSString Average Time: 0.000017

So, as I would have thought, NSString is marginally faster. I imagine that this is because it does need to create and initialize a new object, then send it a message.
     
Senior User
Join Date: Nov 2000
Status: Offline
Reply With Quote
Sep 10, 2002, 10:21 AM
 
So, as I would have thought, NSString is marginally faster. I imagine that this is because it does need to create and initialize a new object, then send it a message.
After looking at the docs...

NSNumber's stringValue calls another NSNumber method which calls an NSString method and that calls another NSString method and that finally creates your string...

I really thought NSNumber would be faster (b4 I read the docs). I didn't say it was though...I just knew one would be.

I did a few quick tests based off yours to see if I could get better times..

NSNumber flopped (I tried everything I could think of). initWithFormat: is slightly faster then stringWithFormat:. Past that, there's nothing worth mentioning.

Thanks for the tests!
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:44 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2