开发者

Only one comma/point in the calculator! Objective-C for iPhone

I'm putting up a CalculatorApp for the iPhone and there开发者_开发知识库 is just one thing that i need to end, the floating-point!

As in normal calculators, i need to do something so that will only permit one "." .

Can you dudes help me?


you have a few ways to go, such as, NSString's rangeOfString method, e.g.

#define FLOATING_POINT_STRING @"."; // set this to @"." or @"," according to the floating point type you want to use
float calculatorText = 45.194; // set this to whatever the label says, or you can skip the float => string conversion as shown below
NSString *text = [NSString stringWithFormat:@"%f", calculatorText];

if ([text rangeOfString:FLOATING_POINT_STRING].location != NSNotFound)
{
    // do nothing, there is a floating point
}
else
{ 
// append FLOATING_POINT_STRING to the label
}

Good luck ;)


Are you using a UIButton for the decimal point button? If so, you could simply disable it as soon as it is pressed. And then of course, re-enable it when "clear" or "equals" or whatever is pressed:

[buttonDecimalPoint setEnabled: NO];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜