nsnumberformatter

NSNumberFormatter rounding to negative zero

坚强是说给别人听的谎言 提交于 2019-12-04 05:43:58
I'm using NSNumberFormatter to format float values as integer strings, i.e. omitting the fractional part. I find it odd, that numbers in range (-0.5, 0) *open interval end up as -0 . As this value will be displayed to the user, I think that negative zero is not appropriate. I have experimented with various combinations of numberStyle and roundingMode without success. Is there a way to configure the NSNumberFormatter to output them as 0 , or do I have to resort to manual correction for that range? I had to do correct this myself. I am using the NSNumberFormatter to display temperature with

NSNumberFormatter numberFromString is adding a small fraction when converting certain numbers

烈酒焚心 提交于 2019-12-04 04:42:55
问题 I'm seeing some strange bugs in my iPhone app that I have narrowed down to my use of NSNumberFormatter. A stripped down example... In Xcode playground I have: let numberFormatter = NSNumberFormatter() //numberFormatter.numberStyle = .DecimalStyle - does not change behavior let numberString = "546000.06" let number: NSNumber = numberFormatter.numberFromString(numberString)! print("number: \(number)") let number1: NSNumber = NSDecimalNumber(string: numberString) print("number1: \(number1)")

Swift 3 and NumberFormatter (.currency) == ¤?

老子叫甜甜 提交于 2019-12-04 03:24:30
Xcode 8.0 (8A218a) GM Target : iOS 10 (Swift 3) Consider the following code: let number = NSDecimalNumber(decimal: 22.4) let numberFormatter = NumberFormatter() numberFormatter.numberStyle = .currency numberFormatter.locale = Locale.current let result = numberFormatter.string(from: number) print(result!) The result is: ¤22.40 (I have no idea what ¤ means.) But if I initialize the locale such as: numberFormatter.locale = Locale(identifier: "en_US") The result will be: $22.40 ... which is what I'd expect in the first place. Notice that this works in a Playground tho: The problem seems to happen

Does NSNumberFormatter.stringFromNumber ever return nil?

淺唱寂寞╮ 提交于 2019-12-04 03:09:02
问题 It seems to me that any valid number can also be expressed as a String , so I don't know why this function returns a String? instead of a String . 回答1: My best guess would be because of the legacy support. This is from the official documentation: The behavior of an NSNumberFormatter object can conform either to the range of behaviors existing prior to OS X v10.4 or to the range of behavior since that release. NSNumberFormatter Class Reference 来源: https://stackoverflow.com/questions/28103113

How to avoid rounding off in NSNumberFormatter

丶灬走出姿态 提交于 2019-12-04 01:57:48
I am trying to have a number string with maximum 2 decimals precision, while rest decimals just trimmed off instead of rounding them up. For example: I have: 123456.9964 I want: 123456.99 -> Just want to trim rest of the decimal places What I have tried is: NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle]; [numberFormatter setMaximumFractionDigits:2]; NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithFloat: 123456.9964]]; NSLog(@"%@", numberAsString); There is nothing to set

Formatting Numbers in Swift 3

邮差的信 提交于 2019-12-03 14:58:55
I want to format number to this: 123.234.234.234 from 123234234234 depends on what the user types into the text field. I don't want to manage currency, it's not about currency, it is about the user has to type in a number and this number should be formatted correctly to be easier to read. Not with a comma, with a dot. I found only currency stuff in the whole research What you are looking for is probably groupingSeparator of NumberFormatter let formater = NumberFormatter() formater.groupingSeparator = "." formater.numberStyle = .decimal let formattedNumber = formater.string(from: number) You

Locale.current reporting wrong language on device

走远了吗. 提交于 2019-12-03 13:20:12
I'm trying to format currency values in an iOS app, and I'm using the current Locale settings on the device to use the appropriate currency formatting. In the simulator, everything seems to run fine: when using currencyFormatter.locale = Locale.current , it takes the right locale settings and prints numbers with the right currency format. On my iPhone however, which is configured in French with French regional settings, I would expect another format to be used (e.g.: 1 234,56 € ). But it does not work, and seems to use an English formatting style (e.g.: €1 234,56 ). In fact, if I print the

Preventing decimals with NSNumberFormatter

送分小仙女□ 提交于 2019-12-03 12:13:20
问题 I have an NSNumberFormatter which I'm trying to use to generate a whole number of GBP (£) from an NSNumber. I keep getting two decimal places regardless of which incantation I try. My code is: NSNumberFormatter *fmtCurrency = [[[NSNumberFormatter alloc] init] autorelease]; [fmtCurrency setNumberStyle: NSNumberFormatterCurrencyStyle]; [fmtCurrency setGeneratesDecimalNumbers:FALSE]; [fmtCurrency setCurrencyCode:@"GBP"]; [fmtCurrency setCurrencySymbol:@"£"]; txtTotal.text = [fmtCurrency

NSLocale currency symbol, show before or after amount value

こ雲淡風輕ζ 提交于 2019-12-03 11:37:56
I am using StoreKit to implement an in app purchase store in my application. I have a custom design and it means that the value of the price should be white and large, and the currency symbol smaller, darker and aligned to the top of the price value. I can get the currency symbol without any problems by using the NSLocale in SKproduct 's priceLocale property, and the value of the price in the price property. My problem is knowing when I should put the currency symbol before the price and when to put it after the price. Examples: $5,99 0,79€ I could easily use the NSNumberFormatter to get this

NSNumber stringValue different from NSNumber value

安稳与你 提交于 2019-12-03 09:02:59
I'm having problems with converting NSNumber to string and string to NSNumber . Here's a sample problem: NSString *stringValue = @"9.2"; NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; NSLog(@"stringvalue:%@",[[formatter numberFromString: stringValue] stringValue]); Output will be: stringvalue:9.199999999999999 I need to retrieve the original value, where, in the example should be 9.2 . On the contrary, when the original string is 9.4 the output is still 9.4 . Do you have any idea how to retrieve the original string value without NSNumber doing anything about it? You are