• 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: Numeric Entry and Formatting With NSNumberFormatter Won't Append Zeros
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Numeric Entry and Formatting With NSNumberFormatter Won't Append Zeros


  • Subject: Re: Numeric Entry and Formatting With NSNumberFormatter Won't Append Zeros
  • From: Fritz Anderson <email@hidden>
  • Date: Mon, 26 Sep 2011 14:33:13 -0500

On 23 Sep 2011, at 6:48 PM, Philip McIntosh wrote:

> I have a calculation project using buttons to input numbers into a UILabel. I want it to format the numbers as they are entered to display grouping separators. It has been a struggle but so far the best I have ben able to achieve is this (I got the basic idea from a post at stackoverflow.com):

A threshold question: Do I understand correctly that the only record you have of the value of your accumulator is the text contents of a label? Why? When you call

>    NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:currentText];

you have no control at all over how the label's text is converted to a decimal. It may or may not be lossy. Better that you keep, as a model object, the decimal value, and avoid repeated round trips through the default parser. (You are careful to specify U.S. grouping and decimal separators for _output_, but the convenience parser you're using will use the separators from the current locale.)

The rule is that you never, ever keep program state in a display object. If you _must_ go through a string (which should still be a model object, and not simply be left lying around in a label), set up an NSNumberFormatter and setGeneratesDecimalNumbers:YES, so you can get some control. You can then keep the formatter in a static variable, and reuse it for repeated conversions in both directions — the round trip will be likelier to preserve your intended value.

I'm also curious to know what you intend by

>    currentText = [currentText stringByReplacingOccurrencesOfString:@"." withString:@"."];

Once you've tamed those issues, see if your

	— F

_______________________________________________

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

  • Follow-Ups:
    • Re: Numeric Entry and Formatting With NSNumberFormatter Won't Append Zeros
      • From: Fritz Anderson <email@hidden>
References: 
 >Numeric Entry and Formatting With NSNumberFormatter Won't Append Zeros (From: Philip McIntosh <email@hidden>)

  • Prev by Date: Re: Problems with NSApp runModalForWindow
  • Next by Date: Re: Numeric Entry and Formatting With NSNumberFormatter Won't Append Zeros
  • Previous by thread: Numeric Entry and Formatting With NSNumberFormatter Won't Append Zeros
  • Next by thread: Re: Numeric Entry and Formatting With NSNumberFormatter Won't Append Zeros
  • Index(es):
    • Date
    • Thread