Xcode [NSString floatValue] Rounding Problem - xcode4

Hi I've a problem with NSString floatValue function, if user insert more than six decimal number floatValue function Round data.
NSString *String = #"9.999999";
NSLog(#"Var Float: %f",[String floatValue]);
Result 9.999999
NSString *String = #"9.9999999";
NSLog(#"Var Float: %f",[String floatValue]);
Result 10 ??????
Someone can help me to solve it?
Thanks in advance!

You can't use float to manage number precision
Use NSDecimalNumber
NSString *Stringa = #"9.9999999";
NSDecimalNumber *Decimal = [[NSDecimalNumber alloc] initWithString:Stringa];
NSLog(#"Var Float: %f",[Decimal floatValue]);
NSLog(#"Var Float: %#",[Decimal stringValue]);

Related

How to plus "NSString" in IOS programming?

How to plus NSString?
Example:
NSString *a=#"hello";
NSString *b=#"world";
NSString *s=#"";
I want:
s = "helloworld";
Thank for helping.
Please use below code.
NSString *s= [NSString stringWithFormat:#"%#%#", a, b];
What you are talking about is concatenating. And you need to use:
NSString *a = #"This ";
NSString *b = #"is a string";
NSString *c= [NSString stringWithFormat:#"%#%#", a, b];
Check out: objective-c strings a guide for beginners for a full list.
You can also use stringByAppendingString: like so -
NSString *completeString = [a stringByAppendingString:b];
NSString *a=#"hello";
NSString *b=#"world";
NSString *s=[NSString stringWithFormat:#"%#%#", a, b];
NSString *completeString = [a stringByAppendingString:b];
This function is Good and Easy...

Binary representation of NSString

I need to someone to send me binary data via NSStream and I will convert it into NSString on my end. What's the binary representation that they need to know (byte size, byte order) in order for me to convert the data successfully back into the same string in NSString?
First get the data and convert it into NSData
Then convert NSData to NSString
NSString* newStr = [[[NSString alloc] initWithData:theData
encoding:NSUTF8StringEncoding] autorelease];
If the data is null-terminated, you should instead use -stringWithUTF8String: to avoid the extra \0 at the end.
NSString* newStr = [NSString stringWithUTF8String:[theData bytes]];
(If you have ARC enabled, remove the -autorelease call.)

Count different characters in NSString

How can I count the number of different characters used in a NSString.
Basically, from the string
Condimentum Consectetur Cras
I would like to get 14 as an answer because:
Condimet scura
We can't assume the NSString's content will always be English; a behaviour similar to [NSString length] for such cases - counting Unicode characters - is, therefore, expected.
NSString *oldString=#"THIS IS OLD STRING.";
NSMutableArray *charArray=[[[NSMutableArray alloc]init ] autorelease];
for (int k1=0; k1<[oldString length]; k1++) {
NSString *ichar = [NSString stringWithFormat:#"%c", [oldString characterAtIndex:k1]];
if (![charArray containsObject:ichar]) {
[charArray addObject:ichar];
}
}
NSMutableString *newString=[[NSMutableString alloc]init];
for (int k2=0; k2<[charArray count]; k2++) {
NSString *ichar =[charArray objectAtIndex:k2];
[newString appendString:ichar];
}
NSLog(#"old string lenght is %d",[oldString length]);
NSLog(#"new string lenght is %d",[newString length]);
Did You try this. I hope it will helps You.

Convert NSMutableArray to String problem

I have got a little problem.
I would like to save an array to a textfile with this code:
for(int aa = 0; aa <= [lines2 count]; aa++) {
content = [[NSString alloc] initWithFormat:#"%#;%#", content, [lines2 objectAtIndex:aa]];
}
NSString *filePath = [[NSBundle mainBundle]pathForResource:#"cart" ofType:#"txt"]; //meine Zeile
[content writeToFile:filePath atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];
But it crashes my app. Can anybody give me an easy example, how to convert an NSMutable Array from
One
Two
Three
to an NSString like
One;Two;Three
Hope somebody can help me... :(
You should be able to do the concatenation using somewhat simpler code as below:
NSMutableString* content = [NSMutableString string];
for (int aa=0; aa < [lines2 count]; aa++){
[content appendString:[NSString stringWithFormat:#"%# ",[lines2 objectAtIndex:aa]]];
}
or, You could use componentsJoinedByString.
Maybe less, instead of less_or_equal?
for(int aa = 0; aa < [lines2 count]; aa++)

Problem with stringByTrimmingCharactersInSet:

I'm doing a very simple trimming from a string. Basically it goes like this:
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *s = [[NSString alloc] initWithString:#"Some other string"];
s = [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(#"%#",s);
[pool drain];
return 0;
}
No matter how many times I check, I can't find a mistake on the code. But the compiler keeps returning:
Running…
2009-12-30 08:49:22.086 Untitled[34904:a0f] Some other string
It doesn't trim the string at all. Is there a mistake in my code, because I can't believe something this simple doesn't work. Thanks!
Edit:
I think I've figured my mistake. stringByTrimmingCharactersInSet only trims the string from the leftmost and rightmost ends of a string. Can anybody confirm this?
You are correct - with your code stingByTrimmingCharactersInSet will trim the left and right whitespace but leave internal whitespace alone.
Note that there's also a memory leak in your code sample:
NSString *s = [[NSString alloc] initWithString:#"Some other string"];
This will leak when you re-assign to s in the next line.

Resources