计算器中只有一个逗号/点!适用于 iPhone 的 Objective-C

发布于 2024-10-18 06:28:03 字数 110 浏览 3 评论 0原文

我正在为 iPhone 建立一个 CalculatorApp,我只需要结束一件事,那就是浮点!

与普通计算器一样,我需要做一些事情,以便只允许一个“。” 。

你们能帮帮我吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

⊕婉儿 2024-10-25 06:28:03

您有几种方法可以选择,例如 NSStringrangeOfString 方法,例如

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

祝你好运;)

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

橪书 2024-10-25 06:28:03

您是否使用 UIButton 作为小数点按钮?如果是这样,您可以在按下它后立即将其禁用。当然,当按下“清除”或“等于”或其他任何按钮时重新启用它:

[buttonDecimalPoint setEnabled: NO];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文