Question

Ive scoured everywhere and haven't found a situation similar to mine.. New to Xcode/Objective C, I am putting together a dual purpose (fraction/decimal) calculator app. Ive found some good stuff on the fractions, and have them calculating correctly. However the decimal is a different story. I can get it to appear with this:

    -(IBAction) pressDecimal
    {
     NSRange range = [display.text rangeOfString:@"."];
                 if ( range.location == NSNotFound ) {
                 display.text = [display.text stringByAppendingString:@"."];
    }

or this:

    -(IBAction) pressDecimal
 {
   [displayString appendString: @"."];
   display.text = displayString;
 }

The latter works great, to DISPLAY it on the calculator. I try to read it in as a slot, a double, and i just can't seem to read it in properly... here is how i get the digits in:

     - (void) workNums: (int) nums

{

currentNumber = currentNumber * 10 + nums;
[displayString appendString:
 [NSString stringWithFormat: @"%i", nums]];
display.text = displayString;

}

    //uses tags from attribute inspector (decimal tagged 10)
  -(IBAction)pressNum:(UIButton *)sendIT
{
   int nums = sendIT.tag;
   [self workNums: nums];
}

Ive looked everywhere, tried changing from floats to doubles ( i can get it to display 0.00's when i play with them) also here is my method to converting the ANSWER that is a FRACTION to a DECIMAL..which works great too:

  - (double) convertToNum
 {
if (denominator != 0)
    return (double) numerator / denominator;
else 
    return NAN;

So again, cleanly stated... How may I get Objective C to see the decimal inputed in the app, and display the answer as a decimal properly.. am i screwed because i started this a mainly a fraction calculator? Thanks from a noob that moved from BASIC to Objective C

EDIT 4/27/12 *** (new to Stack - but please read my comment below.. i am looking to find out why i can't read decimals into my program... I've tried changing from ints (which i know won't work) and doubles and floats, but all i get is 0.00 on user input when running this all as doubles:

     -(IBAction)pressNum:(UIButton *)sendIT
     {
         int nums = sendIT.tag;
         [self workNums: nums];
     }

        - (void) workNums: (int) nums
     {

     currentNumber = currentNumber * 10 + nums;
     [displayString appendString:
     [NSString stringWithFormat: @"%i", nums]];
     display.text = displayString;
     }

So the goal is to be able to CALCULATE a user inputted decimal number...any suggestions on a if/else situation as these numbers build up in an accumulator / and a double for currentNumber and get turned into numerators/denominators later down the line..(which is probably my issue, but it shouldn't be hard, but I'm making it hard, to use a BOOL or something to say, hey..this isn't a fraction, its a decimal - so lets do this - PLEASE HELP!! GOING NUTS!

Was it helpful?

Solution

I may be thinking this is much simpler than it is, but: if you already have a string like @"3.14" in displayString then you can convert that directly to a double:

double doubleValue = [displayString doubleValue];

Would that not be enough to get the double value and perform calculations?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top