Joshua Hart
Joshua Hart

Reputation: 852

Unknown Error Causing Zeros Obj C

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;
}

View

Upvotes: 1

Views: 66

Answers (1)

Carl Veazey
Carl Veazey

Reputation: 18363

One problem is that the fetchActualValue: method will return 0f for the string as you show in your UI.

float daysWorked = [self fetchActualValue:self.daysWorkedTextField.text];

The above line passes the string in the days worked text field (in your case, the string @"28") to the fetchActualValue: method. I suspect the reason is that method parses only numbers conforming to the number style NSNumberFormatterCurrencyStyle. For the days worked calculation, try just sending the floatValue message to daysWorkedTextField.text. That is, replace the above line with:

float daysWorked = [self.daysWorkedTextField.text floatValue];

I have two further points to add - generally, number styles and date styles on formatters should be used to format output to the user, but to format input with them may be error prone. Also, using floating point numbers for financial calculations is highly discouraged.

Upvotes: 2

Related Questions