简体   繁体   English

Integer和NSDecimalNumber的基本数学问题 - 二进制的操作数无效

[英]basic math problem with Integer and NSDecimalNumber - Invalid operands to binary

Man the simplest stuff always seems so crazy in Objective-C to me, anyways I need to do some basic subtraction and multiplication and am stumped. 男人最简单的东西在Objective-C中对我来说似乎总是那么疯狂,无论如何我需要做一些基本的减法和乘法而且难倒。

I have: 我有:

client.PricingDiscount <-- this is an Integer 16 property on a CoreData NSManagedObject
sku.RetailPrice <-- this is a Decimal property on a CoreData NSManagedObject

I am simply trying to get a NSDecimalNumber to display like so: 我只是想让NSDecimalNumber像这样显示:

NSDecimalNumber *showPrice = sku.RetailPrice * (100 - client.PricingDiscount);

I have tried a bunch of different forms of this and cannot figure out what the hell I am doing wrong here. 我已经尝试了一些不同形式的这个,但无法弄清楚我到底做错了什么。

Standard operators cannot be used with NSDecimalNumber 标准运算符不能与NSDecimalNumber一起使用

NSDecimalNumber *one = [NSDecimalNumber decimalNumberWithDecimal:[[NSNumber numberWithInt:100] decimalValue]]; 
NSDecimalNumber *factor = [one decimalNumberBySubtracting:[NSDecimalNumber decimalNumberWithDecimal:[client.PricingDiscount decimalValue]]];
NSDecimalNumber *showPrice = [sku.RetailPrice decimalNumerByMultiplying:factor];

NSDecimalNumber is an object wrapper for a number--you are treating it like a C value. NSDecimalNumber是数字的对象包装器 - 您将其NSDecimalNumber C值。 Instead, try: 相反,尝试:

float price = [sku.retailPrice floatValue] * (100 - [client.pricingDiscount floatValue]);
NSDecimalNumber *showPrice = [NSDecimalNumber numberWithFloat:price];

It's confusing, but NSNumber and NSDecimalNumber are Objective-C wrappers for C types, typically used for storage in container objects such as NSArray (or Core Data). 令人困惑,但NSNumberNSDecimalNumber是C类型的Objective-C包装器,通常用于存储容器对象,如NSArray (或Core Data)。 NSInteger and NSDecimal , on the other hand, are C types ( NSInteger just maps to int ). 另一方面, NSIntegerNSDecimal是C类型( NSInteger只映射到int )。

EDIT: falconcreek's answer is better for avoiding accuracy loss. 编辑: falconcreek的答案更好,以避免准确性损失。 When working with integers, though, you'll typically use unboxed C types. 但是,在使用整数时,通常会使用未装箱的C类型。

NSDecimalNumber *showPrice = sku.RetailPrice * (100 - client.PricingDiscount);

NSDecimalNumber is a class. NSDecimalNumber是一个类。 What you are doing here is calculating a value, and then assigning that to be the value of a pointer to an NSDecimalNumber object. 你在这里做的是计算一个值,然后将其指定为NSDecimalNumber对象的指针值。 This is bad mojo. 这是糟糕的魔力。

I haven't used NSDecimalNumber before, but if you need that particular object, what you want is something like this: 我之前没有使用过NSDecimalNumber ,但是如果你需要那个特定的对象,你想要的是这样的:

NSNumber* number = [NSNumber numberWithFloat:(sku.RetailPrice * (100 - client.PricingDiscount))];
NSDecimal* decimal = [number decimalValue];
NSDecimalNumber* showPrice = [NSDecimalNumber decimalNumberWithDecimal:decimal];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM