My code seems right but for some reason, after I enter the numbers in the fields, every textfield (except days worked) turns into zeros. Can someone please take a look at the coding below and see what I may be doing wrong?
- (IBAction)calculateButtonPressed:(UIButton *)sender
{
float bonusRate = [self fetchActualValue:self.bonusTextField.text];
float deductionsRate = [self fetchActualValue:self.deductionTextField.text];
float dailyRate = [self fetchActualValue:self.dailyRateTextField.text];
float daysWorked = [self fetchActualValue:self.daysWorkedTextField.text];
float grossPay = dailyRate * daysWorked + bonusRate;
float taxRate = .72;
float ssRate = .0620;
float medRate = .0145;
float medgrossrate = grossPay * medRate;
float ssgrossrate = grossPay * ssRate;
float nillRate = 1;
float bonusTax = .75;
float bonusLeft = bonusRate * bonusTax;
float net = medgrossrate + ssgrossrate;
if (medTaxSwitchOutlet.on) medRate = medRate;
else medRate = nillRate;
if (ssTaxSwitchOutlet.on) ssRate = ssRate;
else ssRate = nillRate;
if (fedTaxSwitchOutlet.on) taxRate = taxRate;
else taxRate = nillRate;
float netPay = grossPay * taxRate + bonusLeft - net - deductionsRate;
self.dailyRateTextField.text = [self fetchFormattedTextValue:dailyRate];
self.netIncomeTextField.text = [self fetchFormattedTextValue:netPay];
self.grossIncomeTextField.text = [self fetchFormattedTextValue:grossPay];
self.bonusTextField.text = [self fetchFormattedTextValue:bonusRate];
self.deductionTextField.text = [self fetchFormattedTextValue:deductionsRate];
}
- (NSString*)fetchFormattedTextValue:(float)value{
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithFloat:value]];
return numberAsString;
}
- (float)fetchActualValue:(NSString*)strValue{
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
float num = [[numberFormatter numberFromString:strValue] floatValue];
return num;
}