1

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

nhgrif
  • 61,578
  • 25
  • 134
  • 173
Joshua Hart
  • 772
  • 1
  • 21
  • 31
  • Have you tried setting a breakpoint in the code and then inspecting the values to see if they're as you would expect? – Kitsune Jul 06 '14 at 20:08
  • Yeah, it just freezes and says (lldb). I check the debug log and nothing. Am I going about this wrong? I apologize, im a newbie. – Joshua Hart Jul 06 '14 at 20:38
  • 2
    It freezing and showing `(lldb)` means it stopped at your breakpoint. That's what you want. Now print out values and step through the code. You may wish to find a good tutorial on using the debugger. – rmaddy Jul 06 '14 at 21:17
  • 1
    Do you realise that using float instead of double will introduce noticable errors? – gnasher729 Jul 07 '14 at 01:10

1 Answers1

2

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.

Community
  • 1
  • 1
Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
  • Carl thank you! I ended up changing all the floats (ie: bonusRate, deductionRate, dailyRate, and daysWorked) in the same context and now it's giving me the values I need! I think I understand what you mean with your last two points. This app is for overseas contractors, and it still has a lot of work to be done on it still. Being new to programming, I'm always looking to learn. I guess I need to research the difference between floats and doubles. I haven't used doubles before, but I plan on youtubing it after this reply. – Joshua Hart Jul 07 '14 at 06:58
  • Also, one more question if you don't mind...Now that it calculates the Gross Income and Net Income fields, if I press my action button (Calculate) again, it returns all the fields to zero (if i hit calculate again after initial calculation). Is there a way that I can retain this data, so that if the user changes the value of one field, that it doesn't reset it? I hope i'm not being confusing. – Joshua Hart Jul 07 '14 at 07:01
  • Doubles won't solve the problem, maybe look at NSDecimalNumber. I don't understand your other comment, sorry. – Carl Veazey Jul 07 '14 at 09:15
  • If you enter all the values in the textFields and press calculate, it works. However if you press Calculate one more time, all the fields zero out...except days worked. – Joshua Hart Jul 07 '14 at 11:49
  • It's not clear to me why that would be... I'd suggest starting a new round of debugging, research, and ask a question if necessary... – Carl Veazey Jul 07 '14 at 14:00