NSString stringWithFormat problem - objective-c

I have such a problem:
double calcValue;
NSString *buf = [NSString stringWithFormat:#"%14f", myCalculator.calcValue];
buf = "7.142857";
istead of
buf = "7.1428571428571423";
I tried to format it in such ways: #"%f".
But when I tried #"%14.9f" 9 signs after comma displayed.
Whats the problem? And how can I solve it? Thanx!

Unless I'm misunderstanding, the correct format would be #"%.14f.
What you did, #"%14f" says you want up to 14 digits, before the decimal, but you didn't specify digits after.

I'm not sure why, but stringWithFormat: doesn't seem to format doubles properly. You might try this approach:
double calcValue=7.1428571428571423;
NSString *buf=[[NSNumber numberWithDouble:calcValue] stringValue];
which will set buf to #"7.142857142857142".
For tighter control over number of digits, you could use a NSNumberFormatter.

Related

NSData to NSString returns Null

I have searched. But still couldnt get it.
I'm converting NSdata to NSString.
When I do [data description];
it returns me <00000000 31323334 35363738>
Yes, Im receiving my string #"12345678".
How do I convert it to NSString appropriately?
I tried
NSString *b = [NSString stringWithUTF8String:[data bytes]];
NSString *a = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
Both returns me null.
Any idea?
Thanks
Hi all,
Thanks for all suggestion.
It appears to be constant whereby theres a null character infront always.
So whenever I receive something, i just remove the first <00000000>, then its working fine already
This happens if the encoding is incorrect.
Try using ASCII to test out. ASCII almost certainly work to retrive somekind of string. If it's only numbers it will probably work.
NSString *a = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSASCIIStringEncoding];
Most common except UTF-8 enconding is:
NSASCIIStringEncoding
NSUnicodeStringEncoding
NSISOLatin1StringEncoding
NSISOLatin2StringEncoding
NSSymbolStringEncoding
try them out and see if they work.
I'm converting NSdata to NSString. When I do [data description]; it
returns me <00000000 31323334 35363738> Yes, Im receiving my string
#"12345678".
No -- you aren't receiving that string. You are receiving a byte sequence that starts with a bunch of 0x00 values and is followed by a series of bytes that happen to correspond to the ASCII sequence "12345678".
I.e. you have raw data and are trying to convert it to a constrained type, but can't because the constrained type cannot represent the raw data.
You could try using the "lossy conversion" APIs on NSString, but that might not work and would be fragile anyway.
Best bet?
Only convert the bytes in the NSData that actually represent the string to an instance of NSString. That can be done with -initWithBytes:length:encoding:; you'll need to do the calculations to find the correct offset and length.
This may be because the first bytes of your data is 00. The character 0 is the end of string character. When creating a string from ASCII (from an array of chars or an array of bytes as you are doing), when the character 0 is encountered at the beginning, it produces an empty string.
I would however expect it to return an instance of NSString with 0 characters, and not null.

Replacing a single occurrence of string Obj-c

I am trying to replace only one of the occurrences of a NSString with another string. I know that you can use stringByReplacing*Occurrences*OfString; however, that replaces all occurrences. What I am looking for is more like stringByReplacing*Occurrence*OfString. If anyone could help me that would be great. Thanks,
Jnani
Something like this?
NSRange location = [someString rangeOfString:stringToReplace];
NSString* result = [someString stringByReplacingCharactersInRange:location withString:stringToReplaceWith];

Replace comma with dot when getting floatvalue of NSString

I want the users to be able to input values above 0 for a money amount in my application. So 0,0 or any representation of zero will be unacceptable but 0,1 will be accepted, for example.
In order to do this, I was planning to get the float value of NSString and compare it to 0.0 but this does not work since our decimal numbers need to be seperated with comma, always (due to a business requirement). If the commas were dots, then the comparison does the job.
What's the most decent way to replace commas with dots in my amount texts?
PS: The user is limited to enter only numbers and just one comma, for the decimal part.
And actually I was wondering if this is sthg that could be done with number formatters or so...
Thx in advance
You could just use stringByReplacingOccurrencesOfString
NSString *newString = [ammount stringByReplacingOccurrencesOfString:#"," withString:#"."];
You can use the NSString+JavaAPI category, and then do:
NSString* newString = [myString replace: #"," withString: #"."];
Of course, this may not help if the user happens to enter something like 1,000,00.
Here's a neat way to use NSScanner:
NSScanner *scanner = [NSScanner localizedScannerWithString:theInputString];
float result;
[scanner scanFloat:&result];
Apple recommend to use an NSScanner.
this is the code I use :
NSScanner *scanner;
NSLocale *local =[NSLocale currentLocale ];
[scanner setLocale:local];
float result;
scanner = [NSScanner localizedScannerWithString:<YOUR NSString>];
[scanner scanFloat:&result];

How to Turn Number into a String with Format?

Say I have a number, double, or NSNumber, 3.333333
I want that to turn that into #"3.3"
How would I do so?
Can I do NSString stringWithFormat? But what's the format?
NSString *str = [NSString stringWithFormat:#"%.1f", yourDouble];
or for NSNumber:
NSString *str = [NSString stringWithFormat:#"%.1f", [yourNSNumber doubleValue]];
0.0f means the amount of digits before and after the decimal. So #Wevah's answer would be correct, but keeping that in mind will save you time in the future.
%f stands for a float variable which I am sure you understand. What you would need to display this is a float variable because it contains a decimal. Like people have stated before, you will need to use %.1f
The % just tells the compiler that a special character is coming up.
The f tells the compiler that it is a float variable.
The .1 tells the compiler how many decimal places your float variable is to have. If you would want to have 6 decimal places, then you would us %.6f
Yes, you will want to use string with format.
Say you have a UILabel, then you will want to say
theLabel'sName.text = [NSString stringWithFormat:#"%.1f", ((float)int1 / int2)];
You need the (float) to tell the compiler that whatever int1 / int2 is, is a float variable.
If your NSNumber instance is called myNumber then do
[myNumber stringValue];

Remove extra zeros when converting a double to a string in Objective C

So when I convert a double to a string using something like this:
double number = 1.1;
text = [[NSString alloc] initWithFormat:#"%f", number];
The variable text ends up printing something like this: 1.100000. I realize that this is because of the way the computer stores the value however I need an easy way to remove those extra zeros and, if need be, the decimal as well if the number turns out to an integer.
Thanks!
Try to look at NSNumber and NSNumberFormatter with it's usesSignificantDigits property. It is probably what you are looking for.