Issue adding a "$" to a tip calculator
I'm trying to add a "$" to a tip result in my app, but I'm having some difficulties. I'm next to Xcode and Objective-C, I am not exactly sure where I need to place the symbol with causing an error. Below is a copy of my .m code.
Any help is much appreciated
#import "tipcalcViewController.h"
@implementation tipcalcViewController
- (void)dealloc
{
[super dealloc];
}
- (IBAction)aSliderChanged:(id)sender {
UISlider *slider = (UISlider *)sender;
if (slider == tipslide) {
NSString *t开发者_如何转开发ip = [NSString stringWithFormat:@"%.2f", slider.value * 100];
int tipPercentage = [tip intValue];
NSString *multiplier;
if (tipPercentage < 10) {
multiplier = [NSString stringWithFormat:@"1.0%i", tipPercentage];
}
else if (tipPercentage >= 10) {
multiplier = [NSString stringWithFormat:@"1.%i", tipPercentage];
}
[costWithTipLabel setText:[NSString stringWithFormat:@"%.2f", ([[costWithoutTip text] intValue] * [multiplier floatValue])]];
[tipTextLabel setText:[[NSString stringWithFormat:@"Tip(%i", tipPercentage] stringByAppendingString:@"%):"]];
[self performSelector:@selector(updateNumOfPeop)];
}
else if (slider == peopleslide) {
NSString *p = [NSString stringWithFormat:@"%.f", slider.value*10];
int numberOfPeople = [p intValue];
[numberOfPeopleTextLabel setText:[NSString stringWithFormat:@"Each(%i):", numberOfPeople]];
[numberOfPeopleLabel setText:[NSString stringWithFormat:@"%.2f", [[costWithTipLabel text] floatValue]/numberOfPeople]];
}
[totalBillCost setText:[costWithTipLabel text]];
}
- (void)updateNumOfPeop {
NSString *p = [NSString stringWithFormat:@"%.f", peopleslide.value*10];
int numberOfPeople = [p intValue];
[numberOfPeopleTextLabel setText:[NSString stringWithFormat:@"Each(%i):", numberOfPeople]];
[numberOfPeopleLabel setText:[NSString stringWithFormat:@"%.2f", [[costWithTipLabel text] floatValue]/numberOfPeople]];
}
/*
- (IBAction)aSliderChanged:(id)sender {
UISlider *slider = (UISlider *)sender;
if (slider == tipslide) {
NSString *tip = [NSString stringWithFormat:@"%.f", slider.value * 100];
float tipPercentage = [tip floatValue];
NSString *multiplier = [NSString stringWithFormat:@"1.%.f", tipPercentage];
[costWithTipLabel setText:[NSString stringWithFormat:@"%.2f", [[costWithoutTip text] floatValue] * [multiplier floatValue]]];
[tipTextLabel setText:[[NSString stringWithFormat:@"Tip (%.f", slider.value *100]stringByAppendingString:@"%):"]];
}
else if (slider == peopleslide) {
NSString *p = [NSString stringWithFormat:@"%.f", slider.value*10];
float numOfPeople = [p floatValue];
[numberOfPeopleTextLabel setText:[NSString stringWithFormat:@"Each (%.f):", numOfPeople]];
[numberOfPeopleLabel setText:[NSString stringWithFormat:@"%.2f", [[costWithTipLabel text] floatValue]/numOfPeople]];
}
[totalBillCost setText:[NSString stringWithFormat:@"%.2f", [[costWithTipLabel text] floatValue]]];
}
*/
- (void)viewDidLoad {
[costWithoutTip becomeFirstResponder];
[costWithoutTip setDelegate:self];
[costWithoutTip setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
[tipslide setValue:0.01];
[peopleslide setValue:0.1];
[super viewDidLoad];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[costWithoutTip setText:[NSString stringWithFormat:@"%.2f", [costWithoutTip text].floatValue]];
[costWithoutTip resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[costWithoutTip resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
[costWithoutTip resignFirstResponder];
return YES;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
If you're trying to format currency, use NSNumberFormatter.
NSNumber *someMoney = [NSNumber numberWithFloat:5.95];
NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *formattedAsDollars = [currencyFormatter stringFromNumber:someMoney];
[currencyFormatter release];
You can even venture into the world of NSDecimalNumber if you're feeling brave.
精彩评论