nsnumber

How do you get the int and modulo (mod) of division with an NSDecimalNumber

≡放荡痞女 提交于 2019-12-21 12:16:29
问题 I am confused by NSDecimalNumber and its "behaviors". I have an NSDecimalNumber that represents a dollar value, say $37.50. I'd like to find out how many times say 5.0 goes into that number and then know what's left over. I can get the straight division and get 7.50 but I want 7 mod 2.50. I could convert to an integer but need to save the "cents" so wondering if there's some tricks in the framework? 回答1: Using Peter Hoseys example, but with iOS code: NSDecimalNumber *dividend =

NSNumber compare: returning different results

删除回忆录丶 提交于 2019-12-21 12:13:28
问题 I'm trying to do some number comparisons and I'm getting some weird results. NSNumber* number1 = [NSNumber numberWithFloat:1.004]; NSNumber* number2 = [NSNumber numberWithDouble:1.004]; ([number1 compare:number2] == NSOrderedSame) ? NSLog(@"YES") : NSLog(@"NO"); ([number1 compare:number2] == NSOrderedAscending) ? NSLog(@"YES") : NSLog(@"NO"); ([number1 doubleValue] == [number2 doubleValue]) ? NSLog(@"YES") : NSLog(@"NO"); ([number1 floatValue] == [number2 floatValue]) ? NSLog(@"YES") : NSLog(

Converting Double to NSNumber in Swift Loses Accuracy [duplicate]

社会主义新天地 提交于 2019-12-21 07:56:37
问题 This question already has answers here : Is floating point math broken? (31 answers) Closed 3 years ago . For some reason, certain Doubles in my Swift app are giving me trouble when converting to NSNumber, while some are not. My app needs to convert doubles with 2 decimal places (prices) to NSNumbers so they can be stored and retrieved using Core Data. For example, a few particular prices such as 79.99 would evaluate to 99.98999999999999 unless specifically formatted using NSNumber's

Confusion with distinct and indistinct object

随声附和 提交于 2019-12-20 07:25:11
问题 I thought that NSCountedSet counted numB and numC twice in the frequency because they had the same value, so I created two Fraction objects (not shown) from my class Fraction , and I set their ivars ( numerator , denominator ) to equal each others but the countForObject: treated them as two distinct objects and counted their frequencies as one each. numA and numB pointed to different places in memory but share the same value, and the two Fraction objects pointed to different places in memory

How to convert NSNumber objects for computational purposes?

馋奶兔 提交于 2019-12-19 05:14:19
问题 I an developing some code in which I use a scanner to get to NSNumbers from a string, say x and y. Now I want to compute something simple from x and y, say, z = 10.0/(x + y/60.0)/60.0). I can't do this directly, since the compiler doesn't like ordinary arithmetic symbology applied to number objects. So, I tried defining xD and yD, of type double, and then tried a type conversion xD = (double) x; yD = (double) y; but that also gives a compile error. Just how does one get NSNumber objects

Working with NSNumber & Integer values in Swift 3

馋奶兔 提交于 2019-12-18 18:35:04
问题 I am trying to convert my project to Swift 3.0 however I am having two error messages when working with NSNumber and Integers . Cannot assign type int to type NSNumber for //item is a NSManaged object with a property called index of type NSNumber var currentIndex = 0 for item in self.selectedObject.arrayOfItems { item.index = currentIndex currentIndex += 1 } and even when I change currentIndex to a type NSNumber then I get the error Binary operator '+=' cannot be applied to type 'NSNumber'

How to determine the true data type of an NSNumber?

拟墨画扇 提交于 2019-12-17 09:57:50
问题 Consider this code: NSNumber* interchangeId = dict[@"interchangeMarkerLogId"]; long long llValue = [interchangeId longLongValue]; double dValue = [interchangeId doubleValue]; NSNumber* doubleId = [NSNumber numberWithDouble:dValue]; long long llDouble = [doubleId longLongValue]; if (llValue > 1000000) { NSLog(@"Have Marker iD = %@, interchangeId = %@, long long value = %lld, doubleNumber = %@, doubleAsLL = %lld, CType = %s, longlong = %s", self.iD, interchangeId, llValue, doubleId, llDouble,

Determine if NSNumber is NaN

霸气de小男生 提交于 2019-12-17 09:35:18
问题 How can I determine if a Cocoa NSNumber represents NaN (not a number)? This emerges, for example, when I parse a string that has an invalid (non-numeric) contents. 回答1: So, I found out that the class property [NSDecimalNumber notANumber] is just for this purpose. In some languages NaN != NaN, but this isn't the case in Cocoa. 回答2: As Mike Abdullah says, the natural way to represent a NaN in Cocoa is with nil , but [NSNumber numberWithDouble:NAN] does return a valid object. There is no

How to add two NSNumber objects?

僤鯓⒐⒋嵵緔 提交于 2019-12-17 07:13:45
问题 Now this must be easy, but how can sum two NSNumber ? Is like: [one floatValue] + [two floatValue] or exist a better way? 回答1: There is not really a better way, but you really should not be doing this if you can avoid it. NSNumber exists as a wrapper to scalar numbers so you can store them in collections and pass them polymorphically with other NSObjects . They are not really used to store numbers in actual math. If you do math on them it is much slower than performing the operation on just

NSNumber numberWithInt crashing on numbers >= 13

笑着哭i 提交于 2019-12-14 03:49:47
问题 I'm pretty new to Objective-C. I've read through a similar question but I can't figure out how to solve my problem with that information. Basically, I'm doing this: NSMutableArray* array1 = [[NSMutableArray alloc] initWithCapacity: 1]; NSNumber *n1 = [NSNumber numberWithInt: 12]; [array1 addObject: n1]; NSMutableArray* array2 = [[NSMutableArray alloc] initWithCapacity: 1]; NSNumber *n2 = [NSNumber numberWithInt: 13]; [array2 addObject: n2]; Adding the NSNumber 12 to the array works perfectly