nsdecimalnumber

Convert String to Decimal number it remove points before zeros in Swift 4

爷,独闯天下 提交于 2019-12-13 08:31:03
问题 Please check this example let strValue = "12.00" let decimalValue = Decimal(string: strValue) // 12 it always returns 12 not 12.00 I also try with NSNumber , Float , and Double but it always remove zeros. Can you please help me with this thanks in advance 回答1: Like martin said. 12 and 12.00 is the same number. It doesn't mathematical matter how many 0 are behind the comma. While a number stays the same, the representation is a different topic. String(format: "%.6f", 12) will give 12.000000 as

Creating autorelease objects in iPhone Development

烈酒焚心 提交于 2019-12-13 02:14:54
问题 I have a requirement to create some NSDecimalNumber objects objects as part of my application (as I require the precision of calculation they offer) but I note that in calculations they return NSDecimalNumber objects which are, presumably, autoreleased. My question is really whether this is potentially problematic in an iPhone application where I may carry out lots of calculations. The question is not just relating to NSDecimalNumber specifically but to the sometimes unavoidable creation of

Objective-C - How To Remove Characters From a String?

試著忘記壹切 提交于 2019-12-12 09:16:15
问题 I have a UILabel that has a formatted String (formatted for currency), so there is a dollar sign, $21.34. In the core data entity the attribute is of a type double, I am using an NSDecimalNumber to save to the database. self.purchase.name = self.nameTextField.text; NSString *string = self.amountLabel.text NSDecimalNumber *newAmount = [[NSDecimalNumber alloc] initWithString:string]; NSLog(@"%@", string); // THIS RETURNS NaN, because of dollar sign i think NSManagedObjectContext *context = self

Trouble with NSDecimalNumber's decimalNumberByDividingBy:withBehavior:

若如初见. 提交于 2019-12-11 12:26:43
问题 I always seem to run into trouble with NSDecimalNumber! Today, I get this error: "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFNumber decimalNumberByDividingBy:withBehavior:]: unrecognized selector sent to instance 0xd1fb10'" Here is the source of the error: - (void)setUpInstance { static NSDecimalNumberHandler* roundingBehavior = nil; if (roundingBehavior == nil) { roundingBehavior = [[NSDecimalNumberHandler alloc] initWithRoundingMode

getting the sum of all instances of Entity B from a given instance of Entity A

孤街浪徒 提交于 2019-12-10 23:32:34
问题 I have a to-many relationship between Entity A and Entity B (iOS Core Data app). I need to get the sum of the NSDecimalNumber attribute of all instances of Entity B from a given instance of Entity A. Does anyone have a tutorial or example of this? 回答1: You would use the @sum collection operator thusly: NSNumber *theSum=[ObjectA.objectBs valueForKey:@"@sum.numericalAttribute"]; 来源: https://stackoverflow.com/questions/5251255/getting-the-sum-of-all-instances-of-entity-b-from-a-given-instance-of

Converting from `NSDecimal` or `NSDecimalNumber` to C#'s `decimal` type

狂风中的少年 提交于 2019-12-10 18:49:27
问题 I've got an NSDecimalNumber from StoreKit's SKProduct class, and I want to convert it to C#'s decimal type to minimize loss of precision. Is there a straightforward way to do such a thing? I figure my two choices are: Assume that I understand the binary implementation of each and do my own bit-wise conversion, or Have NSDecimalNumber give me a string and then have decimal parse it. I figure option 1 is way too much work and probably even brittle, so I'm inclined to go with option 2. But that

Turn a NSDecimalNumber negative

纵然是瞬间 提交于 2019-12-10 02:40:03
问题 I am looking for a way to turn a NSDecimalNumber negative by multiplying by -1 . /* decNumber is the one I would like to turn negative */ NSDecimalNumber *decNumber = [values objectAtIndex:billIndex]; NSDecimalNumber *minusOne = [[NSDecimalNumber alloc] initWithInt: -1]; finalValue = [[NSDecimalNumber alloc] initWithDecimal: [[decNumber decimalNumberByMultiplyingBy: minusOne] decimalValue]]; This works but it feels like it's just too much for such a simple logic. Can you think of a better way

How do I add NSDecimalNumbers?

Deadly 提交于 2019-12-06 16:53:56
问题 OK this may be the dumb question of the day, but supposing I have a class with : NSDecimalNumber *numOne = [NSDecimalNumber numberWithFloat:1.0]; NSDecimalNumber *numTwo = [NSDecimalNumber numberWithFloat:2.0]; NSDecimalNumber *numThree = [NSDecimalNumber numberWithFloat:3.0]; Why can't I have a function that adds those numbers: - (NSDecimalNumber *)addThem { return (self.numOne + self.numTwo + self.numThree); } I apologize in advance for being an idiot, and thanks! 回答1: You can't do what you

wrong decimal separator with NSDecimalNumber in iOS

China☆狼群 提交于 2019-12-06 04:38:55
问题 I tried to output the description of a decimal number with the correct decimal separator in the following way: NSString* strValue = @"9.94300"; NSDecimalNumber* decimalNumber = [NSDecimalNumber decimalNumberWithString: strValue]; NSLocale* locale = [NSLocale currentLocale]; NSLog(@"%@", [locale localeIdentifier]); NSLog(@"%@", [decimalNumber descriptionWithLocale: locale] ); The output is: de_DE 9.943 The decimal separator should be ',' instead of '.' for this locale. Where is the error? How

What locale argument to pass to NSDecimalNumber +decimalNumberWithString:locale: so it always works with NSString's using the dot (.) decimal mark?

烈酒焚心 提交于 2019-12-06 01:26:59
问题 I have an NSString which I want to convert into an NSDecimalNumber . The string is received from a server and is always formatted using the en_US locale like XXX.YYY and not like XXX,YYY . I want to create an NSDecimalNumber which accepts XXX.YYY regardless of the locale the user. The number is never displayed to the user, it's used to do internal math. Normally you'd do something like this: NSDecimalNumber *n = [NSDecimalNumber decimalNumberWithString:@"1.234"]; However, if the user is