Iphone 在目标C中使用NSString stringWithFormat显示小数点

Iphone 在目标C中使用NSString stringWithFormat显示小数点,iphone,objective-c,ipad,Iphone,Objective C,Ipad,我对objective C/iPad开发完全是新手,但我正在尝试在iPad上构建一个简单的四功能计算器 我目前最大的困难是在按下十进制按钮时只显示一个。下面是按下按钮(如两个)时发生的情况 -(IBAction)inTwo:(id)sender { display = display*10+2; [resultfield setText:[NSString stringWithFormat:@"%f",display]]; } 试着去做 display = displa

我对objective C/iPad开发完全是新手,但我正在尝试在iPad上构建一个简单的四功能计算器

我目前最大的困难是在按下十进制按钮时只显示一个
。下面是按下按钮(如两个)时发生的情况

-(IBAction)inTwo:(id)sender {
    display = display*10+2;
    [resultfield setText:[NSString stringWithFormat:@"%f",display]];    
}
试着去做

display = display*10+. 
这显然不起作用。有没有办法在显示的末尾加上句点

谢谢

但是,您不能在
显示中添加点,因为它不是字符串


但是,您不能将点添加到
显示中,因为它不是字符串。

如果您只想显示它,可以执行以下操作:

[resultField setText:[NSString stringWithFormat:@"%f%@", display, @"."]];
然后,在其他方法中,让他们在添加之前检查是否有小数点:

-(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]];
    }
}

您在这里要做的是首先检查是否有小数点,以确定是否应将数字添加到小数点的1位或右侧。如果有小数点,我们将数字除以小数点位置与整个字符串长度之差的十倍。

如果您只想显示它,可以执行以下操作:

[resultField setText:[NSString stringWithFormat:@"%f%@", display, @"."]];
然后,在其他方法中,让他们在添加之前检查是否有小数点:

-(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]];
    }
}

您在这里要做的是首先检查是否有小数点,以确定是否应将数字添加到小数点的1位或右侧。如果有小数点,我们将数字除以小数点位置与整个字符串长度之差的十倍。

您只漏掉了一点点:

[resultfield setText:[NSString stringWithFormat:@"%f.", display]];
如果由于某种原因,解析器将%f后面的小数点识别为某种关于浮点或其他精度的说明符(几乎是正数,它不会这样做),只需转义小数点:

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

您只错过了一点点:

[resultfield setText:[NSString stringWithFormat:@"%f.", display]];
如果由于某种原因,解析器将%f后面的小数点识别为某种关于浮点或其他精度的说明符(几乎是正数,它不会这样做),只需转义小数点:

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

我会以与你选择的方式略有不同的方式来做。我会让按钮直接更新显示,只在需要计算时转换成数字

e、 g

然后当你需要计算一些东西时:

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

顺便说一下,考虑把这个数字作为一个数字。NSDecimalNumber上有一些方法可以做简单的算术运算,它们以10为基数工作,因此您可以使用它们并避免浮点数的许多令人讨厌的表示问题。

我会以与您选择的方式略有不同的方式来做。我会让按钮直接更新显示,只在需要计算时转换成数字

e、 g

然后当你需要计算一些东西时:

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

顺便说一下,考虑把这个数字作为一个数字。NSDecimalNumber上有一些方法可以进行简单的算术运算,它们以10为基数工作,因此您可以使用它们,并避免许多令人讨厌的浮点数表示问题。

要设置输出数字的格式,通常应使用
NSNumberFormatter
。只需将其插入
结果字段
,然后通过我的dandy DDMathParser运行它:要格式化输出的数字,通常应该使用
NSNumberFormatter
。只需将其插入
resultfield
,然后通过我的dandy DDMathParser运行它: