nsmutablearray float object returns a zero - iphone

In the root model I have:
[self.rPrices replaceObjectAtIndex:0
withObject:[NSNumber numberWithFloat:(float)nR4]];
NSLog(#"%.2f", [self.rPrices objectAtIndex:0]);
where rPrices is NSMutableArray.
nR4 is not zero but the above NSLog(...); displays zero.
Thanks

Try this
NSLog(#"%.2f", [[self.rPrices objectAtIndex:0] floatValue]);
or alternativly just print the NSNumber as an object
NSLog(#"%#", [self.rPrices objectAtIndex:0]);

NSNumber is an object. So you can't print it using float format specifier "%f".
You can use "%#" or get the float value from it using -floatValue and print it using "%f".

Related

componentsSeparatedByString return wrong result

I used this code to cut the string
NSString *titleString = #"22.225453615805794,113.554006577014889";
NSArray *array = [titleString componentsSeparatedByString:#","];
NSLog(#"title string %#", titleString);
NSLog(#"first %.15f", [[array objectAtIndex:0] floatValue]);
NSLog(#"second %.15f", [[array objectAtIndex:1] floatValue]);
but why it return
22.225454330444336
and
113.554008483886719
Because floating point numbers are not that accurate, you can get a higher accuracy by calling doubleValue instead of floatValue:
NSLog(#"second %.15f", [[array objectAtIndex:1] doubleValue]);
This is not a problem with componentsSeparatedByString:.
I think there is problem in converting string into float. Try using double.

Nsnumber out of scope problem

I am trying to make NSNumber *percent to get the integer value percentint but it keeps making it out of scope.. The Nslog logs are like this:
The value of integer num is 4
The value of integer num is with NsNumber
78910432
My code is this:
In my header file:
int percentint;
NSNumber *percent;
#property(nonatomic,retain) NSNumber *percent; //tried without using this too
In my .m file:
#synthesize percent; //tried without using this too
percentint=4;
NSLog(#"The value of integer num is %i", percentint);
percent= [NSNumber numberWithInt:percentint];
percent= [[NSNumber alloc] initWithInt:percentint]; //tried without using this too.
[percent autorelease]; //tried without using this too.
NSLog(#"The value of integer num is with NsNumber %i", percent);
Try:
percentint=4;
NSLog(#"The value of integer num is %i", percentint);
self.percent= [NSNumber numberWithInt:percentint];
NSLog(#"The value of integer num is in array %#", self.percent);
NSLog(#"The value of integer num is in array %d", [self.percent intValue]);
leaving the #synthesize percent; in.
User #Felz is correct. in order to get the value of a NSNumber, you must retrieve it with a method call:
int percentInt = 95;
NSNumber *percent = [NSNumber numberWithInt:percentInt];
int myint = [percent intValue];
What you did instead, was print out the pointer address for percent
NSLog(#"Percent Value: %d",[percent intValue]);
NSLog(#"Percent Address: 0x%X",percent);
Remember that NSNumber *percent means percent is a pointer not a value.
Like the other user insinuated, NSNumber is not an integer as you are trying to call it in the last NSLog. to get the int value out of a NSNumber, use [percent intValue].
Another side note: you don't need to initialize percent twice. The first call numberWithInt: is like doing an alloc/init/release.
Also, never release an object before you are done with it.

How can I read Float from Core Data / iPhone?

I have Float values stored in Core-Data.
What is the code to use to read these values in an NSstring ?
Core-Data uses NSNumber objects to store the float value.
To get the 'raw' float value and put it into a string you would use something like this.
NSNumber *floatNumber = [managedObject valueForKey:#"myFloatValueKey"];
float myFloat = [floatNumber floatValue];
NSString *floatString = [NSString stringWithFormat:#"%f", myFloat];
Maybe a NSNumberFormatter would be useful.
Well, assuming that you mean your Core Data entity has an attribute of type float, you could simply access that field after you perform a fetch of that object.
[[self managedObjectContext] fetchObjectsForEntityName:#"EntityName" withPredicate:
#"(attribute LIKE[c] 'value') AND (attribute2 > %#)", someValue];
You could then put this in string format with this:
NSString* myNewString = [NSString stringWithFormat:#"%f", [[managedObject floatAttribute] floatVal]];

Problem when converting NSString to NSNumber in iPhone

I am having problem when converting string (YaxisData) to NSNumber. I have to return a NSNumber for Core-plot to get the graph done but its not working. Here's the sample code
-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{
NSNumber *num = [NSNumber numberWithDouble:[[YaxisData objectAtIndex:index] doubleValue]];
return num;
}
num returns junk data such as -1 or 993494949494 but when I log the double value of number, it prints the correct value. I am not able to return this double value as the function signature requires only the NSNumber to be returned.
NSLog(#"Number: %f", [num doubleValue]);
I am stuck here and would really appreciate any help in this regard. Thanks!
Would this give a better result somehow?
NSString *aString = [YaxisData objectAtIndex:index];
NSLog(#"%#", aString);
double value = [aString doubleValue];
NSLog(#"%f", value);
NSNumber *number = [NSNumber numberWithDouble:value];
NSLog(#"%#", number);
If not, could you show the results of the NSLog()'s?
I know that in practice it seems the same code, yet one might not be so sure that -objectAtIndex: always returns a string. It might be a localization issue as well (commas and dots for decimal separator might get mixed up, this would definitely mess up your results. In case of a localization issue, check the following link:
How to convert an NSString into an NSNumber

How can I access the int values of an object in an NSMutableArray?

I try to access some values in a NSMutableArray I created, but I only get some numbers (address?) if I try to access them.
I was able to initialize an array and can add and change objects with
[myNSMutableArray addObject:[NSNumber numberWithInt:10]]
and
[myNSMutableArray replaceObjectAtIndex:0 withObject:[NSNumber numberWithInt:47]
I also can print the value at index [0] with
NSLog(#"%#", [myNSMutableArray objectAtIndex:0]);
and I get 47 as expected.
But how can I access the integer value of the object in the array so I can save it tomyIntValue?
int myIntValue = [[myNSMutableArray objectAtIndex:0] intValue];
Just have a look at the NSNumber class reference