有人可以向我确认这是将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];
的修订版 的 *
PayPalPayment *payment = [[PayPalPayment alloc] init];
payment.amount = [NSDecimalNumber decimalNumberWithString:totalPriceNumber];
payment.currencyCode = @"USD";
payment.shortDescription = @"Hipster t-shirt";
答案 0 :(得分:0)
尝试:
totalPriceNumber = [[NSDecimalNumber alloc] initWithFloat:totalAmount];
答案 1 :(得分:0)
另一种选择是从float
:
NSDecimalNumber *totalPriceNumber = [NSDecimalNumber decimalNumberWithDecimal:[@(totalAmount) decimalValue]];
答案 2 :(得分:0)
根据Apple docs,首选方法是使用+decimalNumberWithString:;希望有所帮助。