nsdecimalnumber

Converting NSDecimalNumber to NSString

…衆ロ難τιáo~ 提交于 2019-12-02 20:43:55
I'm retrieving a key from an object that looks like this: po obj { TypeID = 3; TypeName = Asset; } The key value is being retrieved like this: NSString *typeId = (NSString*)[obj objectForKey:@"TypeID"]; Rather than typeId being an NSString, it is an NSDecimalNumber. Why is that? How do I convert it to an NSString? You need to use the stringWithFormat function: NSString *typeId = [NSString stringWithFormat:@"%@", [obj objectForKey:@"TypeID"]]; or stringValue : NSString *typeId = [[obj objectForKey:@"TypeID"] stringValue]; This should work: NSString *typeId = [[obj objectForKey:@"TypeID"]

Mathematical integrity of NSDecimalNumber

♀尐吖头ヾ 提交于 2019-12-02 05:32:14
I'm using numbers divided by 10^30 I may be adding values like 1000000000000000 and 5000000000000000 stored in NSDecimalNumber s. My concern is that I think I've seen a few times, when adding or subtracting these values, incorrect math being done. Is that a possibility or are NSDecimalNumbers pretty sound in terms of the integrity of their math. In answer to your question, the math offered by Decimal / NSDecimalNumber is sound, and the problem probably rests in either: The calculations might exceed the capacity of these decimal formats (as outlined by rob mayoff). For example, this works

How to add all decimal numbers in an NSMutableArray

孤者浪人 提交于 2019-12-01 12:31:05
I have a NSMutableArray which have some NSDecimalNumber in it, like (500,50.80,70,8000) Now I want to add all those decimal numbers together. I've tried to use for (NSDecimalNumber *number in self.numbersArray) { NSDecimal *sum += [number decimalValue] } But failed. Use - (NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumber Take a look at NSDecimalNumber Class Reference NSDecimalNumber *lNumber = [NSDecimalNumber zero]; for (NSDecimalNumber *number in self.numbersArray) { lNumber = [lNumber decimalNumberByAdding:number]; } A simple way to add all NSNumber s in an array is

NSDecimalNumber multiplication strangeness

ⅰ亾dé卋堺 提交于 2019-12-01 07:16:51
ExclusivePrice, quantity are both NSDecimalNumbers. NSDecimalNumber *price = [exclusivePrice decimalNumberByMultiplyingBy:quantity]; NSLog(@"%@ * %@ = %@", exclusivePrice, quantity, price); The result I get: 2010-04-05 00:22:29.111 TestApp[13269:207] 65 * 2 = -0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007169919476068871316457914368 What I expected: 2010-04-05 00:22:29.111 TestApp[13269:207] 65 * 2 = 130 Can anyone explain this please? Edit: Full repro: NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; [formatter setNumberStyle

Check if NSDecimalNumber is whole number

倖福魔咒の 提交于 2019-11-29 14:45:20
What's the easiest way to check if an instance of NSDecimalNumber is a whole number? Thanks! @interface NSDecimalNumber (IsIntegerNumber) @property (readonly) BOOL isIntegerNumber; @end @implementation NSDecimalNumber (IsIntegerNumber) -(BOOL)isIntegerNumber { NSDecimalValue value = [self decimalValue]; if (NSDecimalIsNotANumber(&value)) return NO; NSDecimal rounded; NSDecimalRound(&rounded, &value, 0, NSRoundPlain); return NSDecimalCompare(&rounded, &value) == NSOrderedSame; } @end yoninja Subtract the whole number from the decimal number. If the difference is zero then the decimal number is

Different ways of comparing NSDecimalNumber

我是研究僧i 提交于 2019-11-29 11:44:26
问题 For example, with primitive, I'll do this if ( (x >= 6000) && (x <= 20000) ) // do something here and with NSDecimalNumber, this is what I have if ( (([x compare:[NSNumber numberWithInt:6000]] == NSOrderedSame) || ([x compare:[NSNumber numberWithInt:6000]] == NSOrderedDescending)) && (([x compare:[NSNumber numberWithInt:20000]] == NSOrderedSame) || ([x compare:[NSNumber numberWithInt:6000]] == NSOrderedAscending)) ) { // do something here } Is there any other ways (easier and more elegant) to

How to round an NSDecimalNumber in swift?

倖福魔咒の 提交于 2019-11-29 09:32:56
I can't find any resources on this, and I've been trying all sorts of stuff, but nothing works. According to Apple's documentation, you round an NSDecimalNumber like this: NSDecimalNumber.decimalNumberByRoundingAccordingToBehavior(<#behavior: NSDecimalNumberBehaviors?#>) It takes in an NSDecimalNumberBehavior, which I'm unsure how to manipulate since it (1) cannot be initiated into a variable and have it's properties changed, and (2) the roundingMode() method according to the documentation doesn't take any parameters, but Xcode fills in a parameter space for "self". I'm totally lost on this.

Check if NSDecimalNumber is whole number

纵饮孤独 提交于 2019-11-28 08:24:10
问题 What's the easiest way to check if an instance of NSDecimalNumber is a whole number? Thanks! 回答1: @interface NSDecimalNumber (IsIntegerNumber) @property (readonly) BOOL isIntegerNumber; @end @implementation NSDecimalNumber (IsIntegerNumber) -(BOOL)isIntegerNumber { NSDecimalValue value = [self decimalValue]; if (NSDecimalIsNotANumber(&value)) return NO; NSDecimal rounded; NSDecimalRound(&rounded, &value, 0, NSRoundPlain); return NSDecimalCompare(&rounded, &value) == NSOrderedSame; } @end 回答2:

How to round an NSDecimalNumber in swift?

南楼画角 提交于 2019-11-28 03:02:52
问题 I can't find any resources on this, and I've been trying all sorts of stuff, but nothing works. According to Apple's documentation, you round an NSDecimalNumber like this: NSDecimalNumber.decimalNumberByRoundingAccordingToBehavior(<#behavior: NSDecimalNumberBehaviors?#>) It takes in an NSDecimalNumberBehavior, which I'm unsure how to manipulate since it (1) cannot be initiated into a variable and have it's properties changed, and (2) the roundingMode() method according to the documentation

How to store 1.66 in NSDecimalNumber

空扰寡人 提交于 2019-11-26 17:56:24
I know float or double are not good for storing decimal number like money and quantity. I'm trying to use NSDecimalNumber instead. Here is my code in Swift playground. let number:NSDecimalNumber = 1.66 let text:String = String(describing: number) NSLog(text) The console output is 1.6599999999999995904 How can I store the exact value of the decimal number 1.66 in a variable? In let number:NSDecimalNumber = 1.66 the right-hand side is a floating point number which cannot represent the value "1.66" exactly. One option is to create the decimal number from a string: let number = NSDecimalNumber