• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSNumberFormatter dropping trailing zeros?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSNumberFormatter dropping trailing zeros?


  • Subject: Re: NSNumberFormatter dropping trailing zeros?
  • From: Fritz Anderson <email@hidden>
  • Date: Mon, 8 Mar 2010 16:30:32 -0600

On 8 Mar 2010, at 3:06 PM, Ryan Stephens wrote:

> Hi all,
>
> I'm trying to use an NSNumberFormatter to format the text of a
> UITextField as a user enters characters.  The issue I'm currently
> having is that the number formatter always drops a trailing zero.  For
> example, see the following code:
>
> - (BOOL)textField:(UITextField *)textField
> shouldChangeCharactersInRange:(NSRange)range
> replacementString:(NSString *)string
> {
>    NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init]
> autorelease];
>    [formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
>    [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
>    [formatter setLocale:[NSLocale currentLocale]];
>
>    NSString *currentValue = [[formatter
> numberFromString:textField.text] stringValue];
>    NSLog(@"current value: %@", currentValue);
> }
>
> Assuming currentLocal == en_US and textField.text == "$2.30",
> [formatter numberFromString:textField.text] returns 2.3.  I'd like it
> to return 2.30

But numberFromString: returns an NSNumber, which represents a numeric value, not a string. There's no difference in _numeric_ value between 2.3 and 2.30. There is a stringValue method for NSNumber, but it's a convenience method that gets you a generic, not-for-a-particular-purpose representation of the value.

The presence, or absence, or having-passed-through of an NSNumberFormatter doesn't change the value of the NSNumber.

If you want to do arithmetic with the NSNumber, you could take the doubleValue and work on that. It would be better for your purposes to work with an NSDecimalNumber; use setGeneratesDecimalNumbers: on the formatter.* Do all your arithmetic with NSDecimalNumber methods, and then use the formatter to convert the result to NSString.

	— F

----
* The docs that introduce the method "discourage" using it, but I don't see a cleaner way to go from string to decimal.

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >NSNumberFormatter dropping trailing zeros? (From: Ryan Stephens <email@hidden>)

  • Prev by Date: Re: [NSTableview] can't make selected text stay black
  • Next by Date: Re: Custom View in Toolbar
  • Previous by thread: NSNumberFormatter dropping trailing zeros?
  • Next by thread: short question but I don't know how to describe it
  • Index(es):
    • Date
    • Thread