nsdecimalnumber

NSDecimalNumber round long numbers

好久不见. 提交于 2021-02-16 13:14:10
问题 I'm trying to get NSDecimalNumber to print out large numbers, 15 or more digits. At 15 digits I see 111,111,111,111,111. Above 15 digits I see 1,111,111,111,111,110 even though the number being formatted is 1111111111111111. An example to illustrate my problem: NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; [formatter setNumberStyle:NSNumberFormatterDecimalStyle]; [formatter setMaximumSignificantDigits:25]; [formatter setUsesSignificantDigits:true]; NSDecimalNumber* test =

NSDecimalNumber to the power of NSDecimalNumber

本秂侑毒 提交于 2021-01-03 05:35:14
问题 I have two NSDecimalNumbers and I need to apply one to the power of the other, originally this code was using doubles and I could compute this with the pow() function like this: double result = pow(value1, value2); The problem I have is I am converting the code to use NSDecimalNumbers and although they include the method toThePowerOf, it only accepts int values. At the moment the only solution I have to this problem is to convert the NSDecimalNumbers Temporarily but this results in a loss of

NSDecimalNumber(x).intValue returns -2, 0, 15 and 199, depending on the amount of decimals in x (x = 199.999…5)

谁都会走 提交于 2020-06-26 04:26:06
问题 We found an interesting case in our business logic that totally breaks our logic and we don't understand why NSDecimalNumber and Decimal behaves the way it does. My playground for the cases is as follows: import Foundation let pQuantity = Decimal(string: "0.2857142857142857")! let pPrice = Decimal(string: "7.00000000000000035")! let calced = NSDecimalNumber(decimal: pQuantity * pPrice * Decimal(integerLiteral: 100)) // 200 let decimal = calced.decimalValue // 199

Swift's Decimal precision issue

随声附和 提交于 2020-01-15 10:39:28
问题 According to the docs here, Swift 3/4 Decimal type is a representation in base 10 bridged to NSDecimalNumber. However I'm having precision issues that do not reproduce when using NSDecimalNumber. let dec24 = Decimal(integerLiteral: 24) let dec1 = Decimal(integerLiteral: 1) let decResult = dec1/dec24*dec24 // prints 0.99999999999999999999999999999999999984 let dn24 = NSDecimalNumber(value: 24) let dn1 = NSDecimalNumber(value: 1) let dnResult = dn1.dividing(by: dn24).multiplying(by: dn24) //

When is it better to use an NSDecimal, NSDecimalNumber instead of a double?

自古美人都是妖i 提交于 2020-01-14 10:27:32
问题 For simple uses, such as tracking weight values like 65.1kg, is there any benefit of going with NSDecimal/NSDecimalNumber over double? My understanding here is double (or even float) provides more than enough precision in such cases. Please correct me if I'm wrong. 回答1: First, read Josh Caswell's link. It it especially critical when working with money. In your case it may matter or may not, depending on your goal. If you put in 65.1 and you want to get exactly 65.1 back out, then you

NSDecimalNumber multiplication strangeness

血红的双手。 提交于 2020-01-11 06:01:19
问题 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:

Why is Swift Decimal Returning Number from String Containing Letters?

雨燕双飞 提交于 2020-01-07 03:16:26
问题 I am working with Swift's Decimal type, trying to ensure that an user-entered String is a valid Decimal . I have two String values, each including a letter, within my Playground file. One of the values contains a letter at the start, while the other contains a letter at the end. I initialize a Decimal using each value, and only one Decimal initialization fails; the Decimal initialized with the value that contains the letter at the beginning. Why does the Decimal initialized with a value that

Why is Swift Decimal Returning Number from String Containing Letters?

人盡茶涼 提交于 2020-01-07 03:15:31
问题 I am working with Swift's Decimal type, trying to ensure that an user-entered String is a valid Decimal . I have two String values, each including a letter, within my Playground file. One of the values contains a letter at the start, while the other contains a letter at the end. I initialize a Decimal using each value, and only one Decimal initialization fails; the Decimal initialized with the value that contains the letter at the beginning. Why does the Decimal initialized with a value that

NSDecimalNumber Math Error in the deep decimals [duplicate]

此生再无相见时 提交于 2020-01-06 08:02:49
问题 This question already has answers here : Mathematical integrity of NSDecimalNumber (2 answers) How to store 1.66 in NSDecimalNumber (1 answer) Closed last year . I have a Swift extension on NSDecimalNumber : extension NSDecimalNumber { var asRawValue: NSDecimalNumber { return self.multiplying(byPowerOf10: 30) } } And a test: let t = NSDecimalNumber(value: 335).asRawValue let u = NSDecimalNumber(value: 0.00001).asRawValue // fails XCTAssert(t.subtracting(u).compare((NSDecimalNumber(value: 334

Converting currency string into NSDecimalNumber

。_饼干妹妹 提交于 2020-01-05 04:37:05
问题 I'm trying to convert some currency string (e.g., $25,000) into NSDecimalNumber using the following code: NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init]; [currencyFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4]; [currencyFormatter setGeneratesDecimalNumbers:TRUE]; [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle]; // textField.text is some currency string (e.g., $25,000) NSDecimalNumber *decimalNumber = [currencyFormatter numberFromString