nsdecimalnumber

how should nsdecimalnumber's be stored in the database?

最后都变了- 提交于 2020-01-04 11:43:33
问题 What is the best way to store nsdecimalnumbers in a database? Numeric types like float, real and double are out, because it is important to preserve the precision for currency usage. The two alternatives I have considered are string and storing two ints, one holding the mantissa and one holding the exponent. The challenge facing the latter approach is that I dont see an easy way to pull the mantissa and exponent out of the nsdecimalnumber. I can get NSDecimal from NSDecimalNumber, but the

iOS 8 number conversion/formatting error? (cannot reproduce)

余生长醉 提交于 2020-01-02 05:02:07
问题 The following method takes input from a UITextField and formats it for display. This code has worked flawlessly for years, but a problem was just reported on the iPhone 6 Plus using iOS 8.1. It happens every time for the user but I have not been able to reproduce it. I believe it has to do with NSNumber/NSDecimalNumber conversions and formatting on iOS 8, perhaps for a 64-bit app/device. The keyboard used for input is a number pad, so the only text that can be entered into the textfield are

NSDecimalNumber decimalNumberWithString: ignores current locale

自闭症网瘾萝莉.ら 提交于 2019-12-31 21:53:28
问题 According to the documentation, [NSDecimalNumber decimalNumberWithString:] should use the locale decimal separator: Whether the NSDecimalSeparator is a period (as is used, for example, in the United States) or a comma (as is used, for example, in France) depends on the default locale. But when I try it, this code: NSLog(@"%@", [NSDecimalNumber decimalNumberWithString:@"100,1"]); NSLog(@"%@", [NSDecimalNumber decimalNumberWithString:@"100,1" locale:NSLocale.currentLocale]); Gives... 100 100.1

Swift: Decode imprecise decimal correctly

雨燕双飞 提交于 2019-12-31 07:33:50
问题 I need to decode ( Decodable protocol) an imprecise decimal value correctly, from this question I understand how to properly handle the Decimal instantiation, but how can I do this when decoding? If trying to init any number as a String if let value = try! container.decode(String.self, forKey: .d) { self.taxAmount = Decimal(string: value) } I get Fatal Error: "Expected to decode String but found a number instead." And if try to init 130.43 as a Decimal if let value = try! container.decode

{NSDecimalNumber integerValue} behaving strangely in iOS8

前提是你 提交于 2019-12-30 06:04:47
问题 OK team, this is weird. [NSDecimalNumber integerValue] is behaving strangely. I'm sat at a breakpoint, trying to figure out why some parts of my app are broken in iOS8, and I'm looking at a variable called "timeSeconds". It appears like this in the Xcode Variables View: _timeSeconds (NSDecimalNumber *) 344.514533996581994496 But when I query it in the debugger, I see this: (lldb) p [self.timeSeconds doubleValue] (double) $14 = 344.51453399658192 (lldb) p [self.timeSeconds intValue] (int) $15

Using NSDecimalNumber for Fibonacci series with Binet's formula

删除回忆录丶 提交于 2019-12-24 00:59:52
问题 I've spent considerable time today trying to calculate the Fibonacci nth term when n is a very large number. I decided to use Objective-C which in hindsight may not have been the best decision, considering how long it has taken. I researched and decided to use Binet's formula which seems to work for other people using other programming languages. double phi = (sqrt(5) + 1) / 2.0; long long j = (long long) round(pow(phi, number) / sqrt(5)); Is the gist of a fibonacci(number) function in C. I

Mathematical integrity of NSDecimalNumber

好久不见. 提交于 2019-12-20 04:29:17
问题 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. 回答1: In answer to your question, the math offered by Decimal / NSDecimalNumber is sound, and the problem probably rests in either: The calculations

How to add all decimal numbers in an NSMutableArray

霸气de小男生 提交于 2019-12-19 11:34:51
问题 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. 回答1: Use - (NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumber Take a look at NSDecimalNumber Class Reference NSDecimalNumber *lNumber = [NSDecimalNumber zero]; for (NSDecimalNumber *number in self

How to add all decimal numbers in an NSMutableArray

孤街醉人 提交于 2019-12-19 11:34:37
问题 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. 回答1: Use - (NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumber Take a look at NSDecimalNumber Class Reference NSDecimalNumber *lNumber = [NSDecimalNumber zero]; for (NSDecimalNumber *number in self

How to store 1.66 in NSDecimalNumber

人盡茶涼 提交于 2019-12-17 04:07:54
问题 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? 回答1: In let number:NSDecimalNumber = 1.66 the right-hand side is a floating point number which cannot represent the