nsdecimalnumber

Fractional Part of NSDecimalNumber

两盒软妹~` 提交于 2019-12-05 22:49:39
问题 I'm using NSDecimalNumber to store a value for currency. I'm trying to write a method called "cents" which returns the decimal portion of the number as an NSString with a leading 0 if the number is < 10. So basically NSDecimalNumber *n = [[NSDecimalNumber alloc] initWithString:@"1234.55"]; NSString *s = [object cents:n]; And I'm trying to craft a method that will return 01, 02, etc...up to 99 as a string. I can't seem to figure out how to return the mantissa as a string in this format. I feel

Turn a NSDecimalNumber negative

社会主义新天地 提交于 2019-12-05 01:53:46
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 to achieve this? You could use NSDecimalNumber>>decimalNumberWithMantissa:exponent:isNegative to

How do I add NSDecimalNumbers?

旧巷老猫 提交于 2019-12-04 22:28:42
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! Frank Krueger You can't do what you want becuase neither C nor Objective C have operator overloading. Instead you have to write:

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

主宰稳场 提交于 2019-12-04 20:59:37
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.purchase.managedObjectContext; NSError *error = nil; if (![context save:&error]) { NSLog(@"Unresolved

NSDecimalNumber zeros following decimal point (iPhone SDK)

a 夏天 提交于 2019-12-04 12:46:02
问题 I have the following code (...) numberStyle = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO]; strThree = textFieldThree.text; strThree = [strThree stringByReplacingOccurrencesOfString:@"." withString:@""]; fieldOneDNa = (NSDecimalNumber *)[NSDecimalNumber decimalNumberWithString:strThree]; oneHundred = (NSDecimalNumber *)[NSDecimalNumber decimalNumberWithString:@"100"];

wrong decimal separator with NSDecimalNumber in iOS

此生再无相见时 提交于 2019-12-04 10:49:21
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 can I output the correct decimal separator depending on the local? @TriPhoenix: Yes I'm running iOS 5.

Swift Exceptions to Exception handling

China☆狼群 提交于 2019-12-04 09:07:41
After perusing through forums and Swift documentation (not completely, I admit), it appears that instead of try-catch mechanisms, in Swift we are encouraged to write code that is more safe from exceptions. In light of that, I have a question about a sample API, and would like to learn how to more safely handle this situation: For example, I can create the following class using the NSDecimalNumberHandler: class MathWhiz { init() { let defaultBehavior: NSDecimalNumberHandler = NSDecimalNumberHandler.defaultDecimalNumberHandler() } func add(op1: String, op2: String) ->NSDecimalNumber { return

Fractional Part of NSDecimalNumber

ぃ、小莉子 提交于 2019-12-04 05:27:19
I'm using NSDecimalNumber to store a value for currency. I'm trying to write a method called "cents" which returns the decimal portion of the number as an NSString with a leading 0 if the number is < 10. So basically NSDecimalNumber *n = [[NSDecimalNumber alloc] initWithString:@"1234.55"]; NSString *s = [object cents:n]; And I'm trying to craft a method that will return 01, 02, etc...up to 99 as a string. I can't seem to figure out how to return the mantissa as a string in this format. I feel like I'm missing a convenience method but I looked at the documentation again and don't see anything

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

[亡魂溺海] 提交于 2019-12-04 05:08:05
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 running the fr_FR locale of Mac OS X, that will break. en_US will interpret it as one point two three four ,

Converting NSDecimalNumber to NSString

大兔子大兔子 提交于 2019-12-03 07:11:50
问题 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? 回答1: You need to use the stringWithFormat function: NSString *typeId = [NSString stringWithFormat:@"%@", [obj objectForKey:@"TypeID"]]; or stringValue : NSString *typeId = [[obj