 |
 |
int, double, float to stringValue??
|
 |
|
 |
|
Senior User
Join Date: Jun 2002
Location: UK
Status:
Offline
|
|
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
|
|
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
|
|
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.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jun 2002
Location: UK
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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! 
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|