I'm a noob to iphone development (2nd day using xcode) and I am having trouble making a dynamic text for a label. My string:
dealerspremiumLabel.text= [NSString stringWithFormat:#"%d%% (%#%d)", premiumPercentage, symbol, premiumCurrency];
is returning as "0% ((null)88988)" instead of "0% ($00.00)". I can't figure out why this is happening because in ViewDidLoad I set my variables as:
premiumPercentage=0;
premiumCurrency= 00.00;
symbol=#"$";
dealerspremiumLabel.text= [NSString stringWithFormat:#"%d%% (%#%d)", premiumPercentage, symbol, premiumCurrency];
My label outlet is properly set and my variables are setup properly as well. I also have checked my Log and the values for these particular variables are set correctly before the above code. Any help figuring this out is greatly appreciated.
Use this way,
currency is float you are using %d instead of %f
dealerspremiumLabel.text= [NSString stringWithFormat:#"%d%% (%#%.2f)", premiumPercentage, symbol, premiumCurrency];
EDIT:
As your symbol is giving null, you have missed to alloc+init.
Or you can directly use :
dealerspremiumLabel.text= [NSString stringWithFormat:#"%d%% ($%.2f)", premiumPercentage, premiumCurrency];
NSInteger premiumPercentage=0;
CGFloat premiumCurrency= 00.00;
NSString *symbol1=#"$";
symbol.text= [NSString stringWithFormat:#"%d%% (%#%f)", premiumPercentage,symbol1,premiumCurrency];
NSLog(#"text=%#",symbol.text);
I used this one and its working perfectly fine in my case and its also showing correct value in UILabel
Related
I have two text fields and user entered the values. I can get the values of textFields like below
NSString *number1 = firstTextField.text;
NSString *number2 = secondTextField.text;
I want to multiply number1 and number2 without converting them into integer or number.I am doing like below
NSExpression *expression = [NSExpression expressionWithFormat:[NSString stringWithFormat:#"%#*%#",number1,number2]];NSLog(#"Multiplication result is----%#",[expression expressionValueWithObject:nil context:nil]);
I don't know if it is correct or not. If it is not correct please give me the suggestions how it can be possible.
By using NSExpression is one way to Multiply/Add/Subtract two number strings without converting them into integer or number.
NSExpression *expression = [NSExpression expressionWithFormat:expressionFormat];
You are using here expressionFormat as [NSString stringWithFormat:#"%#*%#",number1,number2];.
If you want to add or subtract number1 and number2 then replace * with + or -. In this way you have to proceed.
If the question was in an interview.
The interviewers were probably expecting you to write a method to go through both arrays and multiply the characters (converting one by one to integers) or (also identifying the represented character to know the equivalent integer number).
Searching on google there are some examples in different languages.
JAVA
http://csjobinterview.wordpress.com/2012/04/05/string-multiplication/
string multiplication
C++
Multiplying two number arrays
It is a common question in interviews.
I simply used textfields... and used the values that the user input into those textfields... here is one calculation example that has worked for me... in this example the user is trying to find the volume of beachstone they would need to order... the formula is pretty straight forward. Make sure you use the math brackets () to distinguish order of operations...
-(IBAction)calculate_beachstone:(id)sender {
float floatanswer =
([area1.text floatValue])+([area2.text floatValue])+([area3.text floatValue])+([area4.text floatValue])+([area5.text floatValue])+([area6.text floatValue])+([area7.text floatValue])+([area8.text floatValue])+([area9.text floatValue])+([area10.text floatValue]))
*([beachstone_depth.text floatValue])/12)/27);
NSString *stringRectResult=[[NSString alloc]
initWithFormat:#"%.1f",floatanswer];
answer_beachstone.text=stringRectResult;
}
I am adding the text found in the textfield (which user can only use numbers for input - custom keyboard)... in this example I have up to 10 fields which can be added together... then I use the * (multiply) to apply a depth in this example, and convert it back to a text string so I can display the result somewhere else...
In fact, if you write this part answer_beachstone to NSUserDefaults, you can use the result anywhere in different controllers, by calling it back.
If you're interested, here is how I did that...
-(IBAction)save_answer_beachstone:(id)sender {
save_answer_beachstone = [[NSString alloc] initWithString:answer_beachstone.text];
[answer_beachstone setText:save_answer_beachstone];
NSUserDefaults *save = [NSUserDefaults standardUserDefaults];
[save setObject:save_answer_beachstone forKey:#"save_answer_beachstone"];
}
Then I can use the resulting "answer" in any controller inside viewDidLoad...
[answer_beachstone setText:[[NSUserDefaults standardUserDefaults] objectForKey:#"save_answer_beachstone"]];
I know the question was asked 4 years ago, but this formula syntax works for me in a number of ways, in various apps...
You can't multiply two strings. It's not possible.
For this you have to convert it into Integer or int using NSNumber or using:
[secondTextField.text intValue].
NSInteger number = [firstTextField.text integerValue]*[secondTextField.text integerValue];
Say I have some vertice values which I am reading into my app as a NSString :
-7501.6 -6198.2 834.939 -5547.66 -6348.32 2122.65
The values in the source are always 6 figures in length.
I need to pass these exact values to OpenGL. If I try to cast as a float (using NSString floatValue) then, as expected, I get an approximate value for each float due to the inexact nature of a float :
-7501.600098, -6198.200195, 834.939026 -5547.660156, -6348.319824, 2122.649902
Can anyone suggest a way that I can get these values into OpenGL and retain their exact initial integrity ?
Thank you.
maybe this: NSString to NSNumber could help.
See 'Getting Numeric Values' paragraph from NSString Documentation (Apple).
[str floatValue];
[str doubleValue];
Can someone give a code example of how to right pad an NSString in objective-c please?
For example want these strings:
Testing 123 Long String
Hello World
Short
if right padded to a column width of say 12: and then a sting "XXX" is added to the end of each, it would give:
Testing 123 xxx
Hello World xxx
Short xxx
That is a 2nd column would like up.
Adam is on the right track, but not quite there. You do want to use +stringWithFormat:, but not quite as he suggested. If you want to pad "someString" to (say) a minimum of 12 characters, you'd use a width specifier as part of the format. Since you want the result to be left-justified, you need to precede the width specifier with a minus:
NSString *padded = [NSString stringWithFormat:#"%-12#", someString];
Or, if you wanted the result to be exactly 12 characters, you can use both minimum and maximum width specifiers:
NSString *padded = [NSString stringWithFormat:#"%-12.12#", someString];
2nd column of what would line up?
Given that you are on iOS, using HTML or a table view would be far more straightforward than trying to line up characters with spaces. Beyond being programmatically more elegant, it will look better, be more resilient to input data changes over time, and render a user experience more in line with expectations for the platform.
If you really want to use spaces, then you are going to have to limit your UI to a monospace font (ugly for readability purposes outside of specific contexts, like source code) and international characters are going to be a bit of a pain.
From there, it would be a matter of getting the string length (keeping in mind that "length" does not necessarily mean "# of characters" in some languages), doing a bit of math, using substringWithRange: and appending spaces to the result.
Unfortunately, Objective-C does not allow format specifiers for %#. A work-around for padding is the following:
NSString *padded = [NSString stringWithFormat:#"%#%*s", someString, 12-someString.length, ""];
which will pad the string to the right with spaces up to a field length of 12 characters.
%-# does not work, but %-s works
NSString *x = [NSString stringWithFormat:#"%-3#", #"a" ];
NSString *y = [NSString stringWithFormat:#"%-3#", #"abcd" ];
NSString *z = [NSString stringWithFormat:#"%-3# %#", #"a", #"bc" ];
NSString *zz = [NSString stringWithFormat:#"%-3s %#", "a", #"bc" ];
NSLog(#"[%#][%#][%#][%#].......", x,y,z,zz);
output:
[a][abcd][a bc][a bc].......
Try below. its working for me.
NSString *someString = #"1234";
NSString *padded = [someString stringByPaddingToLength: 16 withString: #"x" startingAtIndex:0];
NSLog(#"%#", someString);
NSLog(#"%#", padded);
First up, you're doing this a bad way. Please use separate labels for your two columns and then you will also be able to use proportional fonts. The way you're going about it you should be looking for an iPhone curses library.
If you really have to do it this way just use stringWithFormat:, like:
NSString *secondColumnString = #"xxx";
NSString *spacedOutString = [NSString stringWithFormat:#"testingColOne %#", secondColumnString];
NSString *spacedOutString = [NSString stringWithFormat:#"testingAgain %#", secondColumnString];
I have an iPhone app and I want to display a price. So I use these lines of code:
NSNumberFormatter *price = [[NSNumberFormatter alloc] init];
[price setNumberStyle:NSNumberFormatterCurrencyStyle];
[price setCurrencyCode:#"USD"];
What I want is only the price, formatted for the currencyCode but in my case without USD.
So instead of 30,00 $, I want to have only 30,00 without $.
How can I do this?
Best Regards, Tim.
What you're looking to get rid of is the Currency Symbol.
[price setCurrencySymbol:#""];
You can use the setCurrencySymbol to an empty string which should do the trick:
[price setCurrencySymbol:#""];
Please keep in mind that this only sets the local currency code and it might not work with foreign currencies.
Here's a way to do it while keeping the correct locale formatting for the symbol:
_fmt.positiveFormat = [_fmt.positiveFormat
stringByReplacingOccurrencesOfString:#"¤" withString:#""];
_fmt.negativeFormat = [_fmt.negativeFormat
stringByReplacingOccurrencesOfString:#"¤" withString:#""];
The ¤ character is used in the currency formats for the currency symbol. If you replace the currency symbol as suggested in some of the other answers, the formatter does a US standard format for the number (AFAIK - tested in iOS 4 and 5 in the USA). You'll need to do this replacement after setting the locale for the formatter. Try with the currency code HUF which does not have pennies.
Im trying to do something that I think is super simple.
i have 3 integers - prevgues1 , 2 and 3
and i have 3 UILabels prevguess1, 2 and 3
the ints have 1 less s.
When I set the text of the label so
prevguess1.text = [NSString stringWithFormat:#"%d", prevguess1]
prevguess2.text = [NSString stringWithFormat:#"%d", prevguess2];
prevguess3.text = [NSString stringWithFormat:#"%d", prevguess3];
the label just set to a number like 89324 or something like that.
I just don't know what my problem is.
Any ideas would be helpful
Cheers
Sam
Note:
I have tried setting the text simply to a string - and have had luck.
but when i set it to a integer, which start as 0 value, (in viewdidload) the weirdness happens
prevguess1.text = [NSString stringWithFormat:#"%d", prevguess1];
Here you treat prevguess1 as a label and as an integer - probably there's a typo somewhere?
Even if it's not a matter of your problems I think you should consider changing the way you name your variables a bit to avoid possible confusion.