nsnumberformatter

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 =

Convert Double to Scientific Notation in swift

故事扮演 提交于 2021-02-16 07:50:59
问题 I am trying to convert a given double into scientific notation, and running into some problems. I cant seem to find much documentation on how to do it either. Currently I am using: var val = 500 var numberFormatter = NSNumberFormatter() numberFormatter.numberStyle = NSNumberFormatterStyle.ScientificStyle let number = numberFormatter.numberFromString("\(val)") println(number as Double?) // Prints optional(500) instead of optional(5e+2) What am I doing wrong? 回答1: You can set NumberFormatter

Convert Double to Scientific Notation in swift

好久不见. 提交于 2021-02-16 07:50:27
问题 I am trying to convert a given double into scientific notation, and running into some problems. I cant seem to find much documentation on how to do it either. Currently I am using: var val = 500 var numberFormatter = NSNumberFormatter() numberFormatter.numberStyle = NSNumberFormatterStyle.ScientificStyle let number = numberFormatter.numberFromString("\(val)") println(number as Double?) // Prints optional(500) instead of optional(5e+2) What am I doing wrong? 回答1: You can set NumberFormatter

Using Swift how to convert a string to a number

感情迁移 提交于 2021-01-02 02:24:10
问题 I am getting values back from a web service that gives me back prices in a string format, this is put into a Dictionary, so I get prices back as "1.5000" for example, which is obviously 1.50 in currency. However for the life of me I cannot get anything to work in Swift to format this correctly. In most other languages you can do this in a couple of seconds, so I'm getting a bit frustrated with something that is so simple. Here's my test code: var testnumber = "1.5000" let n =

Using Swift how to convert a string to a number

一个人想着一个人 提交于 2021-01-02 02:22:30
问题 I am getting values back from a web service that gives me back prices in a string format, this is put into a Dictionary, so I get prices back as "1.5000" for example, which is obviously 1.50 in currency. However for the life of me I cannot get anything to work in Swift to format this correctly. In most other languages you can do this in a couple of seconds, so I'm getting a bit frustrated with something that is so simple. Here's my test code: var testnumber = "1.5000" let n =

NumberFormatter wrong result for 0.9972

前提是你 提交于 2020-04-19 05:43:18
问题 I have a double: let value = 0.99720317490866084 And a Double extention function: extension Double { func stringWithFixedFractionDigits(min: Int, max: Int) -> String { let formatter = NumberFormatter() formatter.minimumFractionDigits = min formatter.maximumFractionDigits = max formatter.minimumIntegerDigits = 1 let numberObject = NSNumber(value: self) return formatter.string(from: numberObject) ?? "\(self)" } } If I use: value.stringWithFixedFractionDigits(min: 2, max: 2) I get 1.00 but I

iOS: Remove thousand separator in NSDecimalNumber for edit

删除回忆录丶 提交于 2020-01-14 05:10:27
问题 I have decimal numbers stored in my database as decimal. And I display them according to the user's locale: - (NSString *) getLocalizedCurrencyStringWithDigits { NSNumberFormatter *numberFormatter =[[NSNumberFormatter alloc] init]; [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; [numberFormatter setLocale:[NSLocale currentLocale]]; [numberFormatter setMinimumFractionDigits:2]; [numberFormatter setMaximumFractionDigits:2]; NSString *numberString = [numberFormatter

how to use persian numbers in UItextField in swift 3?

心已入冬 提交于 2020-01-11 13:16:55
问题 Hi I am using text field in my swift3 application - I want when user enter a number in the text field instead of english number textfield shows persian numbers - my textfield keyboard has set to just enter number so the user can't use text inside the text field with keyboard so here is my codes that doesn't work override func viewDidLoad() { super.viewDidLoad() let numberFormatter = NumberFormatter() numberFormatter.locale = Locale(identifier: "fa") numberFormatter.numberStyle =

how to use persian numbers in UItextField in swift 3?

▼魔方 西西 提交于 2020-01-11 13:16:06
问题 Hi I am using text field in my swift3 application - I want when user enter a number in the text field instead of english number textfield shows persian numbers - my textfield keyboard has set to just enter number so the user can't use text inside the text field with keyboard so here is my codes that doesn't work override func viewDidLoad() { super.viewDidLoad() let numberFormatter = NumberFormatter() numberFormatter.locale = Locale(identifier: "fa") numberFormatter.numberStyle =

Using NSNumberFormatter to pad spaces between a currency symbol and the value

对着背影说爱祢 提交于 2020-01-05 11:14:25
问题 Apologies if this is a dumb question but I'm trying to format a currency value for my iphone app and am struggling to left-justify the currency symbol, but right-justify the value. So, "$123.45" is formatted as (say) $ 123.45 depending on format-width. This is a kind of accounting format (I think). I've tried various methods with NSNumberFormatter but can't get what I need. Can anyone advise on how to do this? Thanks Fitto 回答1: You're looking for the paddingPosition property of