0

working on an app that tracks an item's price. It's setup as a double in core data, and accepts user input into a text field where they'll type the item's price using a decimal pad. here's the code that sets and stores the item price:

if let price = priceField.text {
            item.price = (price as NSString).doubleValue
        }

what I want to do is set the how the price displays price values. right now if the user types "25.10", the price displays as "$25.1", missing the 0 in the second decimal spot. if the user types "25", the price displays "$25.0" also missing the second decimal spot.

I've tried using a method like what you'd use with a string to set decimal places, but no luck since this is a double.

any suggestions welcome. thanks again for all the help for a novice.

CW_2323
  • 41
  • 1
  • 4
  • If your using Swift, you should be using String and not NSString. – Martheli Sep 09 '17 at 17:27
  • 1
    Use `NumberFormatter` with the correct options to convert that number to a string. You cannot add zeros to a number. However the user never sees a number, they always see a *string*. – Sulthan Sep 09 '17 at 17:39
  • Answer here: https://stackoverflow.com/questions/24051314/precision-string-format-specifier-in-swift – Martheli Sep 09 '17 at 17:44
  • Does the output have to be a double? or can it be a string? – Martheli Sep 09 '17 at 19:43
  • it could be either, technically, but I set up the attributes of price to be a double instead of string. – CW_2323 Sep 09 '17 at 19:50

0 Answers0