nsnumberformatter

Rounding off decimal fraction in ios

ぃ、小莉子 提交于 2019-12-08 15:56:18
问题 NSNumberFormatter *format = [[NSNumberFormatter alloc]init]; [format setNumberStyle:NSNumberFormatterDecimalStyle]; [format setRoundingMode:NSNumberFormatterRoundHalfUp]; [format setMaximumFractionDigits:2]; NSString *temp = [format stringFromNumber:[NSNumber numberWithFloat:23.4451]]; NSLog(@"%@",temp); I need round off a six decimal fraction number to two decimal. In iOS I tried the code above. I get the answer 23.5 when I give input as 23.45, but when I give input 23.445 the answer is 23

How to show only the first two digits with NSNumberFormatter?

喜欢而已 提交于 2019-12-08 11:50:17
问题 I have a number for an example 1329094. I want to get only the first two digits that I make see 13 I create NSNumberFormatter like func setReceivedChart(description: [String], value: [Double]) { var descriptionString: String! var formatterNumber: NSNumber! var chartDataEntryArray = [ChartDataEntry]() for item in 0..<description.count { let chartEntry = ChartDataEntry(value: value[item], xIndex: item) chartDataEntryArray.append(chartEntry) descriptionString = description[item] formatterNumber

Issues with setMaximumFractionDigits

懵懂的女人 提交于 2019-12-07 10:04:56
问题 setMaximumFractionDigits is not working with the following code. NSString *input=@"80.90"; NSNumberFormatter *numberFormatter=[[NSNumberFormatter alloc] init]; [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; [numberFormatter setMaximumFractionDigits:2]; NSNumber *number=[numberFormatter numberFromString:input]; number gets converted to 80.90000000000001. but I have put a limit to 2 decimal places. So, how do I get number as 80.90, instead of 80.90000000000001 回答1:

iOS dial assist auto formats local number to USA number

别来无恙 提交于 2019-12-06 12:29:46
问题 in my application am allowing the user to place call to customer support using this line of code [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt:18002096006"]]]; but iOS formats this to a USA number like below and makes call to USA +1 (800) 209-6006 but this is an Indian toll free number. .... I have seen the same problem discussed from a ios user, and resolved work around is to disable "Dial Assist" in Settings > Phone the problem is,

NSNumberFormatter rounding to negative zero

六眼飞鱼酱① 提交于 2019-12-06 00:54:38
问题 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

Singleton and NSNumberFormatter in Swift

喜夏-厌秋 提交于 2019-12-05 23:58:55
Currently, I have the following code in one of my methods: let formatter = NSNumberFormatter() formatter.numberStyle = .DecimalStyle formatter.currencyGroupingSeparator? formatter.minimumFractionDigits = 2 Because I have to repeat these in various functions in different view controllers, how do I create a singleton in Swift to call for the NSNumberFormatter and avoid duplicates? I assume that I have to create a new Swift file, but unsure of how to construct the class? update: Xcode 8.2.1 • Swift 3.0.2 extension Double { static let twoFractionDigits: NumberFormatter = { let formatter =

NSNumberFormatter Currency Without Symbol?

佐手、 提交于 2019-12-05 10:26:19
问题 I am using NSNumberFormatter to get a currency value from a string and it works well. I use this code to do so: NSNumberFormatter *nf = [[NSNumberFormatter alloc] init]; [nf setNumberStyle:NSNumberFormatterCurrencyStyle]; NSString *price = [nf stringFromNumber:[NSNumber numberWithFloat:[textField.text floatValue]]]; However, it always gives me a currency symbol at the start of the string. Rather than doing it manually form my given string, can I not somehow have the formatter not give the

Formatting Numbers in Swift 3

大城市里の小女人 提交于 2019-12-04 22:56:15
问题 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 回答1: What you are looking for is probably groupingSeparator of NumberFormatter let formater = NumberFormatter() formater

Locale.current reporting wrong language on device

六眼飞鱼酱① 提交于 2019-12-04 21:22:37
问题 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

Trying to format a UITextField to behave like a currency calculator for only numeric input in iOS

有些话、适合烂在心里 提交于 2019-12-04 10:35:00
问题 I have a UITextField in my application that is supposed to receive only numeric input from the user. This numeric input is supposed to represent currency (i.e. have only two decimal places), have the default value of 0.00 when nothing has been entered. I also would like to have a "$" sign that is on the far left, with the number right aligned, such that the number moves from right to left like such: e.g. when entering the number "1234.56" . 1. $ 0.00 . 2. $ 0.01 . 3. $ 0.12 . 4. $ 1.23 . 5. $