开发者

Display decimal point with NSString stringWithFormat in Objective C

I'm a complete novice to objective C/iPad development, but I am trying to build a simple four function calculator on the iPad.

My current biggest difficulty is simply displaying a . when the decimal button is pressed. Here is what happens when a button (like two) is pressed.

-(IBAction)in开发者_开发百科Two:(id)sender {
    display = display*10+2;
    [resultfield setText:[NSString stringWithFormat:@"%f",display]];    
}

Trying to just do

display = display*10+. 

Is obviously not working. Any way to append a period to the end of display?

Thanks


If you just want to display it you can do this:

[resultField setText:[NSString stringWithFormat:@"%f%@", display, @"."]];

Then, in your other methods, have them check to see if there is a decimal point before adding:

-(IBAction)inTwo:(id)sender {
    if([resultField.text rangeOfString:@"."] == NSNotFound) {
        display = display*10+2;
        [resultfield setText:[NSString stringWithFormat:@"%f",display]];
    }
    else {
        display += 2/10 * (resultField.length - [resultField.text rangeOfString:@"."] +1)
        [resultfield setText:[NSString stringWithFormat:@"%f",display]];
    }
}

What you are doing here is first checking to see if there is a decimal point to see if you should be adding the digit to the ones place, or to the right of the decimal. If there is a decimal point, we add the digit divided by ten times the difference between the location of the decimal and the length of the whole string.


resultfield.text = [result.text stringByAppendingString:@"."];

You can't add a dot to display though, since it's not a string.


You've only missed it by a little bit:

[resultfield setText:[NSString stringWithFormat:@"%f.", display]];

If for some reason the parser is identifying the decimal point after the %f as some kind of specifier about the accuracy of the float or something (nearly positive it doesn't do this), just escape the decimal point:

[resultfield setText:[NSString stringWithFormat:@"%f\.", display]];


I would do it slightly differently to the way you have chosen. I'd have the buttons update the display directly and only convert to a number when needed for calculation.

e.g.

-(IBAction)inTwo:(id)sender 
{
    NSString* displayedText = [resultField text];
    [resultfield setText:[NSString stringByAppendingString: @"2"]];
}

-(IBAction)inDecimal:(id)sender 
{
    if (![self isDecimalAlreadyPressed]) // only allowed one decimal in the number
    {
        NSString* displayedText = [resultField text];
        [resultfield setText:[NSString stringByAppendingString: @"."]]; // actually want the localised character really
        [self setDecimalAlreadyPressed: YES];
    }
}

Then when you need to calculate something:

-(double) displayAsDouble
{
    NSDecimalNumber* displayAsDecimal = [NSDecimalNumber decimalNumberWithString: [resultField text]];
    return [displayAsDecimal doubleValue];
}

By the way, consider leaving the number as an NSDecimalNumber. There are methods on NSDecimalNumber to do simple arithmetic and they work in base 10, so you can use them and avoid a lot of the pesky representation issues with floating point numbers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜