• 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: Getting a float from an NSNumberFormatter
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Getting a float from an NSNumberFormatter


  • Subject: Re: Getting a float from an NSNumberFormatter
  • From: Greg Titus <email@hidden>
  • Date: Sat, 4 Jan 2003 15:12:26 -0800

On Saturday, January 4, 2003, at 02:32 PM, Joshua S Emmons wrote:

So here's my question: how do I get the float I want from the NSTextField above?
[myTextField floatValue] is not working?

Honestly, I didn't think to try it. I was thinking it would try to convert the $ and , stuff to a float and fail. Wouldn't I need to get the value from the formatter. Something like
[myFormatter contriveFloatValue]
?? Or is the formatter hooked to the cell in some way I'm not groking?

Well, trying this seems to work for all values I can come up with. So thank you for solving my idiotic question. But if there's a kind soul out there who has a good handle on how formatters work, maybe you can mail me privately with an explanation, because I'm clearly not getting it and the docs are leading me in circles.

The way things work is that the text field uses the formatter to convert between an object value and a string. You feed the text field an object value (-setObjectValue:), the text field asks the formatter to convert it into a string, then displays the string. The user edits the string as desired. Then when you ask the text field for its object value (-objectValue), the text field takes its current text contents, asks the formatter to convert it into a value, and hands it back to you.

The type of value for an NSNumberFormatter is an NSDecimalNumber. When you ask a text field for its -floatValue instead of its -objectValue, it takes the object value (which, again, it gets from the formatter, which does all the parsing required), and calls -floatValue on that _value_. NSDecimalNumber responds to the -floatValue method, and you get the correct result.

If you didn't have a formatter, then the object value would be just the string you entered, and NSString's more simplistic version of -floatValue would get called, try to convert the "$" and ",", and fail. (It would end up with zero.)

Hope this helps,
- Greg
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: Getting a float from an NSNumberFormatter (From: Joshua S Emmons <email@hidden>)

  • Prev by Date: Re: Checking OS Version with Cocoa
  • Next by Date: Re: Long conditional
  • Previous by thread: Re: Getting a float from an NSNumberFormatter
  • Next by thread: Re: Delete Icon
  • Index(es):
    • Date
    • Thread