Ios NSDecimalNumber的字符串无效

Ios NSDecimalNumber的字符串无效,ios,objective-c,nsstring,nsdecimalnumber,Ios,Objective C,Nsstring,Nsdecimalnumber,有人能帮我确认这是将NSString转换为NSDecimalNumber的正确方法吗?我有一个标签,当你点击按钮时,价格会显示出来,这叫做totalPriceCalculated,然后我还有所有计算的字符串。提前谢谢 - (IBAction)calculateTotalPrice:(id)sender { NSString *priceStringOne = [hiddenPriceOneTF text]; float priceFloatOne = [priceStringOn

有人能帮我确认这是将NSString转换为NSDecimalNumber的正确方法吗?我有一个标签,当你点击按钮时,价格会显示出来,这叫做totalPriceCalculated,然后我还有所有计算的字符串。提前谢谢

- (IBAction)calculateTotalPrice:(id)sender {
    NSString *priceStringOne = [hiddenPriceOneTF text];
    float priceFloatOne = [priceStringOne NSNumberFormatter];

    NSString *priceStringTwo = [hiddenPriceTwoTF text];
    float priceFloatTwo = [priceStringTwo floatValue];

    NSString *priceStringThree = [hiddenPriceThreeTF text];
    float priceFloatThree = [priceStringThree floatValue];

    NSString *priceStringFour = [hiddenPriceFourTF text];
    float priceFloatFour = [priceStringFour floatValue];

    NSString *quanityStringOne = [quanityFirstTF text];
    float quanityFloatOne = [quanityStringOne floatValue];

    NSString *quanityStringTwo = [quanitySecondTF text];
    float quanityFloatTwo = [quanityStringTwo floatValue];

    NSString *quanityStringThree = [quanityThirdTF text];
    float quanityFloatThree = [quanityStringThree floatValue];

    NSString *quanityStringFour = [quanityFourthTF text];
    float quanityFloatFour = [quanityStringFour floatValue];

    float totalAmount = priceFloatOne * quanityFloatOne + priceFloatTwo * quanityFloatTwo + priceFloatThree * quanityFloatThree + priceFloatFour * quanityFloatFour ;
    NSString *result = [NSString stringWithFormat:@" $ %0.2f", totalAmount];

    [totalPriceCalculated setText:result];

    NSString *totalPrice = totalPriceCalculated.text;
    NSDecimalNumber *totalPriceNumber = (NSDecimalNumber *)totalPrice;
}

NSString *priceStringOne = [hiddenPriceOneTF text];

float priceFloatOne = [priceStringOne NSNumberFormatter];
更新*

尝试:


totalPriceNumber=[[NSDecimalNumber alloc]initWithFloat:totalAmount]

另一个选项是从浮点创建十进制数:


根据研究,首选的方法是使用;希望有帮助。

您不能使用cast将对象从一个类转换为另一个类。顺便说一句,由于前导$,从字符串转换将不起作用。此外,不要使用stringWithFormat:将十进制值转换为字符串以显示给用户。相反,请使用NSNumberFormatter。这将确保值的格式适合用户的区域设置。另外,不要使用floatValue转换用户输入的数字。同样,使用NSNumberFormatter将文本解析为浮点。这允许用户根据自己的区域设置以熟悉的格式输入数字。我曾尝试更新此设置,但收到一个错误消息,称NSString的No Visible@interface声明了选择器NSNumberFormat。我忘了什么吗。如果我读对了你的评论,这就是我得到的NSString*priceStringOne=[hiddenPriceOneTF text];float priceFloatOne=[priceStringOne NSNumberFormatter];还添加到上述代码中,因此更易于阅读。您使用NSNumberFormatter的尝试甚至不接近正确。使用NSNumberFormatter进行一些搜索。我尝试了这种方法,但当我将totalPriceNumber添加到要插入它的值的位置时,仍然会出现一个错误,即不兼容的指针类型将NSDecimalNumber发送到NSString类型的参数。我更新了上面的代码,以显示我要将totalPriceNumber放在哪里。它进入PayPal iOS代码,看看你想做什么。totalPriceNumber已经是一个NSDecimalNumber,您将其视为一个NSString,不必要地尝试创建另一个NSDecimalNumber。只需执行以下操作:payment.amount=totalPriceNumber;。太棒了,谢谢!我刚开始学习iOS编码,所以我对NSDecimalNumber一无所知。因此,我将在NSNumberFormat上进行一些搜索。ThanksI建议您退一步,找一些关于Objective-C编程语言和编程基础的好教程。现在学习基础知识将为您节省大量时间解决此类问题。这里的问题不是缺乏对NSDecimalNumber类的理解,而是缺乏对基本编程知识的理解。祝你好运
PayPalPayment *payment = [[PayPalPayment alloc] init];
payment.amount = [NSDecimalNumber decimalNumberWithString:totalPriceNumber];
payment.currencyCode = @"USD";
payment.shortDescription = @"Hipster t-shirt";
NSDecimalNumber *totalPriceNumber = [NSDecimalNumber decimalNumberWithDecimal:[@(totalAmount) decimalValue]];