Questions tagged [nsnumberformatter]

A Cocoa class for formatting and parsing numerical values. Use this tag for questions about using `NSNumberFormatter`.

NSNumberFormatter is a highly customizable class that allows converting NSNumber instances to NSString, and vice versa. It is very useful for parsing and/or formatting any kind of numerical values such as currency values.

Resource

563 questions
91
votes
10 answers

Formatting input for currency with NSNumberFormatter in Swift

I am creating a budget app that allows the user to input their budget as well as transactions. I need to allow the user to enter both pence and pounds from separate text fields and they need to be formatted together with currency symbols. I have…
user3746428
  • 11,047
  • 20
  • 81
  • 137
66
votes
9 answers

How to input currency format on a text field (from right to left) using Swift?

I have a number let’s say 0.00. When the user taps 1. We should have 0.01 When the user taps 2. We should display 0.12 When the user taps 3. We should display 1.23 When the user taps 4. We should display 12.34 How can I do that with Swift?
Bolo
  • 2,668
  • 4
  • 27
  • 34
40
votes
21 answers

NSNumberFormatter and 'th' 'st' 'nd' 'rd' (ordinal) number endings

Is there a way to use NSNumberFormatter to get the 'th' 'st' 'nd' 'rd' number endings? EDIT: Looks like it does not exist. Here's what I'm using. +(NSString*)ordinalNumberFormat:(NSInteger)num{ NSString *ending; int ones = num % 10; int…
jan
  • 893
  • 1
  • 9
  • 18
38
votes
3 answers

Limit a double to two decimal places without trailing zeros

I read this and that. I want this exactly: 1.4324 => "1.43" 9.4000 => "9.4" 43.000 => "43" 9.4 => "9.40" (wrong) 43.000 => "43.00" (wrong) In both questions the answers points to NSNumberFormatter. So it should be easy to achieve, but not for…
testing
  • 19,681
  • 50
  • 236
  • 417
34
votes
6 answers

how to move cursor in UITextField after setting its value

can anybody help me with this: i need to implement UITextField for input number. This number should always be in decimal format with 4 places e.g. 12.3456 or 12.3400. So I created NSNumberFormatter that helps me with decimal places. I am setting the…
user1135839
  • 563
  • 2
  • 7
  • 14
29
votes
3 answers

NSNumberFormatter PercentStyle decimal places

I'm using Swift let myDouble = 8.5 as Double let percentFormatter = NSNumberFormatter() percentFormatter.numberStyle = NSNumberFormatterStyle.PercentStyle percentFormatter.multiplier = 1.00 let myString =…
Fred Faust
  • 6,696
  • 4
  • 32
  • 55
23
votes
7 answers

Formatting number as percentage

I don't understand how NSNumberFormatterPercentStyle works! Example: NSNumber *number = [NSNumber numberWithFloat:90.5]; NSNumberFormatter *percentageFormatter = [[[NSNumberFormatter alloc] init] autorelease]; [percentageFormatter…
Mustafa
  • 20,504
  • 42
  • 146
  • 209
22
votes
5 answers

NSNumberFormatter with comma decimal separator

I tried to convert an NSString like "12000.54" into "12.000,54". I wrote an NSNumberFormatter instance. NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease]; [formatter…
Pierre
  • 10,593
  • 5
  • 50
  • 80
22
votes
4 answers

NSNumberFormatter Currency Without Symbol?

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 =…
Josh Kahane
  • 16,765
  • 45
  • 140
  • 253
20
votes
7 answers

iOS - Showing currency without cents using NSNumberFormatter

I'm able to convert a particular NSNumber into currency using NSNUmberFormatter. Its showign $ symbol, numbers separated by "," etc. But I don't want to display cents. For example its displaying $537,335.32 I just want to display $536,335 I'm…
Satyam
  • 15,493
  • 31
  • 131
  • 244
20
votes
6 answers

Preventing decimals with NSNumberFormatter

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…
Echilon
  • 10,064
  • 33
  • 131
  • 217
20
votes
7 answers

Using NSNumberFormatter to get a decimal value from an international currency string

It seems that the NSNumberFormatter can't parse Euro (and probably other) currency strings into a numerical type. Can someone please prove me wrong. I'm attempting to use the following to get a numeric amount from a currency…
ApeOnFire
  • 649
  • 2
  • 6
  • 11
18
votes
9 answers

Locale.current reporting wrong language on device

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 =…
Romain
  • 3,718
  • 3
  • 32
  • 48
16
votes
7 answers

How do I force a sign-character on the output of an NSNumberFormatter

I want to use a number formatter to generate my output, so the number is automatically formatted for the user's locale, but I want it to work like "%+.1f" does in printf(), that is always have a sign specified. NSNumberFormatter *nf =…
LavaSlider
  • 2,494
  • 18
  • 29
14
votes
3 answers

Formatting a number to show commas and/or dollar sign

I want to format my UILabel with commas or better with a dollar sign and commas (with no decimal). Here is the code I am using: IBOutlet UILabel *labelrev float rev = (x + y) labelrev.text = [[NSString alloc] initWithFormat:@%2.f",rev]; I get…
Peter B
  • 169
  • 1
  • 1
  • 6
1
2 3
37 38