Note that there are some explanatory texts on larger screens.

plurals
  1. POStruggling with currency in Cocoa
    primarykey
    data
    text
    <p>I'm trying to do something I'd think would be fairly simple: Let a user input a dollar amount, store that amount in an NSNumber (NSDecimalNumber?), then display that amount formatted as currency again at some later time. </p> <p>My trouble is not so much with the setNumberStyle:NSNumberFormatterCurrencyStyle and displaying floats as currency. The trouble is more with how said numberFormatter works with this UITextField. I can find few examples. <a href="https://stackoverflow.com/questions/276382/best-way-to-enter-numeric-values-with-decimal-points">This thread</a> from November and <a href="https://stackoverflow.com/questions/317311/obtaining-an-nsdecimalnumber-from-a-locale-specific-string">this one</a> give me some ideas but leaves me with more questions.</p> <p>I am using the UIKeyboardTypeNumberPad keyboard and understand that I should probably show $0.00 (or whatever local currency format is) in the field upon display then as a user enters numerals to shift the decimal place along:</p> <ul> <li>Begin with display $0.00</li> <li>Tap 2 key: display $0.02 </li> <li>Tap 5 key: display $0.25</li> <li>Tap 4 key: display $2.54</li> <li>Tap 3 key: display $25.43</li> </ul> <p>Then [numberFormatter numberFromString:textField.text] should give me a value I can store in my NSNumber variable. </p> <p>Sadly I'm still struggling: Is this <em>really</em> the best/easiest way? If so then maybe someone can help me with the implementation? I feel UITextField may need a delegate responding to every keypress but not sure what, where and how to implement it?! Any sample code? I'd greatly appreciate it! I've searched high and low...</p> <p><strong>Edit1:</strong> So I'm looking into NSFormatter's <strong>stringForObjectValue:</strong> and the closest thing I can find to what benzado recommends: <strong>UITextViewTextDidChangeNotification</strong>. Having really tough time finding sample code on either of them...so let me know if you know where to look?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload