Only show my float number to a certain decimal place - Swift - ios

basically I want to remove the decimal places for my step calories burnt calculation. I know I need to add something to self.total2.text = ("\(String(bmi)) kcal") but just need a quick bit of advice on achieving this.
#IBAction func calc2(_ sender: Any) {
if self.value111.text! != "" && self.value222.text! != "" {
let textfieldInt = Float(value111.text!)
let textfield2Int = Float(value222.text!)
var bmi:Float = (((textfieldInt! * 2.204623) * 0.5) / 1500) * textfield2Int!
self.total2.text = ("\(String(bmi)) kcal")
let banner = StatusBarNotificationBanner(title: "Calculation Done! Submit to save. ✅", style: .warning)
banner.show(queuePosition: .front)
}
}
#IBAction func submit2(_ sender: Any) {
let dict = (["kcal": self.total.text!, "date": self.getDate()])
ref?.child("user").child(Auth.auth().currentUser!.uid).child("measurements").child("calculations").child("kcal").child("\(getDate())").setValue(dict)
total.text = ""
let banner = NotificationBanner(title: "Success, Step Calories Burnt has been saved today ⚖️", style: .success)
banner.show(queuePosition: .front)
}

Use a NumberFormatter (have a look here)
let bmi:Float = (((textfieldInt! * 2.204623) * 0.5) / 1500) * textfield2Int!
let formatter = NumberFormatter()
//Set the min and max number of digits to your liking, make them equal if you want an exact number of digits
formatter.minimumFractionDigits = 1
formatter.maximumFractionDigits = 3
self.total2.text = formatter.string(from: NSNumber(value: bmi)) + " kcal"

Have a look at the NumberFormatter class, which lets you fine-tune the string representation of a numeric value, whether integer or floating-point. Set up the NumberFormatter to produce the output in the format you want, then call string(from:) to create a string from your number.

Related

how to ignore a value of a UITextField that has already been calculated in swift 3?

I'm trying to make an app that is very basic. One part of the app is that there are 4 textFields and a button that calculates the sum of these textFields.
The problem that I'm facing is that say I type the value 10 in the first textField then I press the button. The result would be 10. However, if I press it again ( without typing anything in the other textFields), the result would be 20!! Furthermore, if I type 20 in one of the other textFields, the result would be 40!!
The result SHOULD BE 30 NOT 40!!
one possible option I thought of (haven't tried it yet) is assigning 0 to all of the textFields when pressing the button. But I'd like the app to be smarter and keep tracks of the result.
if it helps, here's the code inside the button that calculates the sum:
#IBAction func calBtnPressed(_ sender: UIButton) {
var benifit:[Double] = []
var textFields: [Double] = []
if initialBalance.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = initialBalance.text {
// these lines of code will convert arabic numbers to English ones in case the user uses Arabic number
let initialStr: String = temp
let initialFormatter: NumberFormatter = NumberFormatter()
initialFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let initialFinal = initialFormatter.number(from: initialStr)
benifit.append(Double(initialFinal!))
}
}
if income.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = income.text {
// these lines of code will convert Arabic numbers to English ones in case the user uses Arabic number
let incomeStr: String = temp
let incomeFormatter: NumberFormatter = NumberFormatter()
incomeFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let incomeFinal = incomeFormatter.number(from: incomeStr)
benifit.append(Double(incomeFinal!))
}
}
if salaries.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = salaries.text {
let salariesStr: String = temp
let salariesFormatter: NumberFormatter = NumberFormatter()
salariesFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let salariesFinal = salariesFormatter.number(from: salariesStr)
textFields.append(Double(salariesFinal!))
}
}
if tools.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = tools.text {
let toolsStr: String = temp
let toolsFormatter: NumberFormatter = NumberFormatter()
toolsFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let toolsFinal = toolsFormatter.number(from: toolsStr)
textFields.append(Double(toolsFinal!))
}
}
if maintinance.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = maintinance.text {
let maintinanceStr: String = temp
let maintinanceFormatter: NumberFormatter = NumberFormatter()
maintinanceFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let maintinanceFinal = maintinanceFormatter.number(from: maintinanceStr)
textFields.append(Double(maintinanceFinal!))
}
}
if other.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = other.text {
let otherStr: String = temp
let otherFormatter: NumberFormatter = NumberFormatter()
otherFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let otherFinal = otherFormatter.number(from: otherStr)
textFields.append(Double(otherFinal!))
}
}
for textField in textFields {
sumExpenses += textField
}
for ben in benifit{
sumBenifit += ben
}
totalExpenses.text = String(sumExpenses)
totalAfterSubtractingExpenses.text = String( sumBenifit - sumExpenses )
sumBenifit -= sumExpenses
}
I think I found your problem.
You use a variable sumBenefit which isn't declared in your func, so I assume it is declared in your UIViewController.
Since it is an instance variable, it will not reset each time you click the button.
If you want to reset the values of sumExpenses and sumBenefits each time the button is pressed, then you'll have to do something like this:
sumExpenses = 0
for textField in textFields {
sumExpenses = Int(textField.text)!
}
sumBenefit = 0
for ben in benefit {
sumBenefit += ben
}
I am also making the assumption that you want a number from your textField in the first for-loop, because if sumExpenses is of type Int (or any other number for that matter) then sumExpenses += textField will not compile. You need to take the text of that textField and convert it to an Int.
Again, I am still not super clear what you are trying to do, but please let me know if this works for you, or if you need further clarification.

How to format a Double into Currency - Swift 3

I'm new to Swift programming and I've been creating a simple tip calculator app in Xcode 8.2, I have my calculations set up within my IBAction below. But when I actually run my app and input an amount to calculate (such as 23.45), it comes up with more than 2 decimal places. How do I format it to .currency in this case?
#IBAction func calculateButtonTapped(_ sender: Any) {
var tipPercentage: Double {
if tipAmountSegmentedControl.selectedSegmentIndex == 0 {
return 0.05
} else if tipAmountSegmentedControl.selectedSegmentIndex == 1 {
return 0.10
} else {
return 0.2
}
}
let billAmount: Double? = Double(userInputTextField.text!)
if let billAmount = billAmount {
let tipAmount = billAmount * tipPercentage
let totalBillAmount = billAmount + tipAmount
tipAmountLabel.text = "Tip Amount: $\(tipAmount)"
totalBillAmountLabel.text = "Total Bill Amount: $\(totalBillAmount)"
}
}
You can use this string initializer if you want to force the currency to $:
String(format: "Tip Amount: $%.02f", tipAmount)
If you want it to be fully dependent on the locale settings of the device, you should use a NumberFormatter. This will take into account the number of decimal places for the currency as well as positioning the currency symbol correctly. E.g. the double value 2.4 will return "2,40 €" for the es_ES locale and "¥ 2" for the jp_JP locale.
let formatter = NumberFormatter()
formatter.locale = Locale.current // Change this to another locale if you want to force a specific locale, otherwise this is redundant as the current locale is the default already
formatter.numberStyle = .currency
if let formattedTipAmount = formatter.string(from: tipAmount as NSNumber) {
tipAmountLabel.text = "Tip Amount: \(formattedTipAmount)"
}
How to do it in Swift 4:
let myDouble = 9999.99
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = .currency
// localize to your grouping and decimal separator
currencyFormatter.locale = Locale.current
// We'll force unwrap with the !, if you've got defined data you may need more error checking
let priceString = currencyFormatter.string(from: NSNumber(value: myDouble))!
print(priceString) // Displays $9,999.99 in the US locale
You can to convert like that: this func convert keep for you maximumFractionDigits whenever you want to do
static func df2so(_ price: Double) -> String{
let numberFormatter = NumberFormatter()
numberFormatter.groupingSeparator = ","
numberFormatter.groupingSize = 3
numberFormatter.usesGroupingSeparator = true
numberFormatter.decimalSeparator = "."
numberFormatter.numberStyle = .decimal
numberFormatter.maximumFractionDigits = 2
return numberFormatter.string(from: price as NSNumber)!
}
i create it in class Model
then when you call , you can accecpt it another class , like this
print("InitData: result convert string " + Model.df2so(1008977.72))
//InitData: result convert string "1,008,977.72"
you can create an Extension for either string or Int, I would show an example with String
extension String{
func toCurrencyFormat() -> String {
if let intValue = Int(self){
let numberFormatter = NumberFormatter()
numberFormatter.locale = Locale(identifier: "ig_NG")/* Using Nigeria's Naira here or you can use Locale.current to get current locale, please change to your locale, link below to get all locale identifier.*/
numberFormatter.numberStyle = NumberFormatter.Style.currency
return numberFormatter.string(from: NSNumber(value: intValue)) ?? ""
}
return ""
}
}
link to get all locale identifier
The best way to do this is to create an NSNumberFormatter. (NumberFormatter in Swift 3.) You can request currency and it will set up the string to follow the user's localization settings, which is useful.
As an alternative to using a NumberFormatter, If you want to force a US-formatted dollars and cents string you can format it this way:
let amount: Double = 123.45
let amountString = String(format: "$%.02f", amount)
As of Swift 5.5, you can do this with the help of .formatted:
import Foundation
let amount = 12345678.9
print(amount.formatted(.currency(code: "USD")))
// prints: $12,345,678.90
This should support most common currency code, such as "EUR", "GBP", or "CNY".
Similarly, you can append locale to .currency:
print(amount.formatted(
.currency(code:"EUR").locale(Locale(identifier: "fr-FR"))
))
// prints: 12 345 678,90 €
In addition to the NumberFormatter or String(format:) discussed by others, you might want to consider using Decimal or NSDecimalNumber and control the rounding yourself, thereby avoid floating point issues. If you're doing a simple tip calculator, that probably isn't necessary. But if you're doing something like adding up the tips at the end of the day, if you don't round the numbers and/or do your math using decimal numbers, you can introduce errors.
So, go ahead and configure your formatter:
let formatter: NumberFormatter = {
let _formatter = NumberFormatter()
_formatter.numberStyle = .decimal
_formatter.minimumFractionDigits = 2
_formatter.maximumFractionDigits = 2
_formatter.generatesDecimalNumbers = true
return _formatter
}()
and then, use decimal numbers:
let string = "2.03"
let tipRate = Decimal(sign: .plus, exponent: -3, significand: 125) // 12.5%
guard let billAmount = formatter.number(from: string) as? Decimal else { return }
let tip = (billAmount * tipRate).rounded(2)
guard let output = formatter.string(from: tip as NSDecimalNumber) else { return }
print("\(output)")
Where
extension Decimal {
/// Round `Decimal` number to certain number of decimal places.
///
/// - Parameters:
/// - scale: How many decimal places.
/// - roundingMode: How should number be rounded. Defaults to `.plain`.
/// - Returns: The new rounded number.
func rounded(_ scale: Int, roundingMode: RoundingMode = .plain) -> Decimal {
var value = self
var result: Decimal = 0
NSDecimalRound(&result, &value, scale, roundingMode)
return result
}
}
Obviously, you can replace all the above "2 decimal place" references with whatever number is appropriate for the currency you are using (or possibly use a variable for the number of decimal places).
extension String{
func convertDoubleToCurrency() -> String{
let amount1 = Double(self)
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .currency
numberFormatter.locale = Locale(identifier: "en_US")
return numberFormatter.string(from: NSNumber(value: amount1!))!
}
}
In 2022 using Swift 5.5, I created extensions that convert Float or Double into a currency using your device's locale or the locale you pass as an argument. You can check it out here https://github.com/ahenqs/SwiftExtensions/blob/main/Currency.playground/Contents.swift
import UIKit
extension NSNumber {
/// Converts an NSNumber into a formatted currency string, device's current Locale.
var currency: String {
return self.currency(for: Locale.current)
}
/// Converts an NSNumber into a formatted currency string, using Locale as a parameter.
func currency(for locale: Locale) -> String {
let numberFormatter = NumberFormatter()
numberFormatter.usesGroupingSeparator = locale.groupingSeparator != nil
numberFormatter.numberStyle = .currency
numberFormatter.locale = locale
return numberFormatter.string(from: self)!
}
}
extension Double {
/// Converts a Double into a formatted currency string, device's current Locale.
var currency: String {
return NSNumber(value: self).currency(for: Locale.current)
}
/// Converts a Double into a formatted currency string, using Locale as a parameter.
func currency(for locale: Locale) -> String {
return NSNumber(value: self).currency(for: locale)
}
}
extension Float {
/// Converts a Float into a formatted currency string, device's current Locale.
var currency: String {
return NSNumber(value: self).currency(for: Locale.current)
}
/// Converts a Float into a formatted currency string, using Locale as a parameter.
func currency(for locale: Locale) -> String {
return NSNumber(value: self).currency(for: locale)
}
}
let amount = 3927.75 // Can be either Double or Float, since we have both extensions.
let usLocale = Locale(identifier: "en-US") // US
let brLocale = Locale(identifier: "pt-BR") // Brazil
let frLocale = Locale(identifier: "fr-FR") // France
print("\(Locale.current.identifier) -> " + amount.currency) // default current device's Locale.
print("\(usLocale.identifier) -> " + amount.currency(for: usLocale))
print("\(brLocale.identifier) -> " + amount.currency(for: brLocale))
print("\(frLocale.identifier) -> " + amount.currency(for: frLocale))
// will print something like this:
// en_US -> $3,927.75
// en-US -> $3,927.75
// pt-BR -> R$ 3.927,75
// fr-FR -> 3 927,75 €
I hope it helps, happy coding!
extension Float {
var localeCurrency: String {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = .current
return formatter.string(from: self as NSNumber)!
}
}
amount = 200.02
print("Amount Saved Value ",String(format:"%.2f", amountSaving. localeCurrency))
For me Its return 0.00!
Looks to me Extenstion Perfect when accessing it return 0.00! Why?
Here's an easy way I've been going about it.
extension String {
func toCurrency(Amount: NSNumber) -> String {
var currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = .currency
currencyFormatter.locale = Locale.current
return currencyFormatter.string(from: Amount)!
}
}
Being used as follows
let amountToCurrency = NSNumber(99.99)
String().toCurrency(Amount: amountToCurrency)
Here's how:
let currentLocale = Locale.current
let currencySymbol = currentLocale.currencySymbol
let outputString = "\(currencySymbol)\(String(format: "%.2f", totalBillAmount))"
1st line: You're getting the current locale
2nd line: You're getting the currencySymbol for that locale. ($, £, etc)
3rd line: Using the format initializer to truncate your Double to 2 decimal places.

Decimal number in text field

I'm trying to get a decimal number from a text field. It only can be a decimal number but if I enter something like 'o,5', than the bullets will spawn a lot faster than every 0.5 second.
My code:
#IBAction func enemyBulletDelayClick(_ sender: AnyObject) {
dismissKeyboard()
let correctNumber = enemyBulletDelayText.text?.replacingOccurrences(of: ",", with: ".")
enemyBulletDelay = Double(correctNumber!)!
enemyBulletDelayText.text = ""
}
(I'm converting each ',' to a '.' for the decimal numbers.)
Otherwise it would give me an error.
I tried to use this code and it worked!
Code:
let formatter = NumberFormatter()
formatter.numberStyle = NumberFormatter.Style.decimal
enemySpawnDelay = (formatter.number(from: enemySpawnDelayText.text!)?.doubleValue)!
If you have a ? you need to unwrap, not put !
There are a bunch of ways to remove . afterwards. Pick whatever you want. This is more focused on the process of what you're doing and then you can decide on using NSNumberFormatter or whatever you want to do.
guard let enemyBulletDelayString = enemyBulletDelayText.text? else {
//put whatever you want to do here if this check doesn't pass
return
}
let numberFormatter = NumberFormatter()
formatter.numberStyle = numberFormatter.Style.decimal
if let formattedNumber = numberFormatter.number(from: enemyBulletDelayString) {
enemySpawnDelay = formattedNumber.doubleValue
} else {
numberFormatter.decimalSeparator = ","
if let formattedNumber = numberFormatter.number(from: enemyBulletDelayString) {
enemySpawnDelay = formattedNumber.doubleValue
}
}
This should work with what you want to do.

Choosing units with MeasurementFormatter

This is similar to a question I asked yesterday but the answer I got doesn't seem to work in this case.
I'm getting altitude values in meters from Core Location. I want to display these in a localized form. As an example, the altitude where I am right now is 1839m above sea level. This should be displayed as 6033 feet. The best I can do with MeasurementFormatter is "1.143 mi".
let meters : Double = 1839
let metersMeasurement = Measurement(value: meters, unit: UnitLength.meters)
let measurementFormatter = MeasurementFormatter()
measurementFormatter.locale = Locale(identifier: "en_US")
let localizedString = measurementFormatter.string(from: metersMeasurement)
The .naturalScale option that answered my previous question doesn't help here. I think this is a limitation of the framework, but I wonder if anyone has a workaround for now.
You just need to convert your UnitLength from meters to feet. You can also create a custom US measurement formatter to display it as needed:
extension Measurement where UnitType == UnitLength {
private static let usFormatted: MeasurementFormatter = {
let formatter = MeasurementFormatter()
formatter.locale = Locale(identifier: "en_US")
formatter.unitOptions = .providedUnit
formatter.numberFormatter.maximumFractionDigits = 0
formatter.unitStyle = .long
return formatter
}()
var usFormatted: String { Measurement.usFormatted.string(from: self) }
}
Playground
let value: Double = 1839
let meters: Measurement<UnitLength> = .init(value: value, unit: .meters)
let feet = meters.converted(to: .feet)
let formatted = feet.usFormatted
print(formatted) // "6,033 feet"\n
I think you are correct there's no way to specify this kind of context. You could do something like:
extension MeasurementFormatter
{
func altitudeString(from measurement: Measurement<UnitLength>) -> String
{
var measurement = measurement
let unitOptions = self.unitOptions
let unitStyle = self.unitStyle
self.unitOptions = .naturalScale
self.unitStyle = .long
var string = self.string(from: measurement)
if string.contains(self.string(from: UnitLength.miles))
{
self.unitStyle = unitStyle
measurement.convert(to: UnitLength.feet)
self.unitOptions = .providedUnit
string = self.string(from: measurement)
}
else if string.contains(self.string(from: UnitLength.kilometers))
{
self.unitStyle = unitStyle
measurement.convert(to: UnitLength.meters)
self.unitOptions = .providedUnit
string = self.string(from: measurement)
}
else
{
self.unitStyle = unitStyle
string = self.string(from: measurement)
}
self.unitOptions = unitOptions
return string
}
}
Maybe there are other culturally specific ways of measuring elevation, but this would seem better than miles and kilometers.

How can I format currency depending on decimal value?

I am using NSDecimalNumber to format currency and want the following inputs and outputs:
9.99 --> 9.99
10 --> 10
10.00 --> 10
9.90 --> 9.90
9.9 --> 9.90
0 --> 0
0.01 --> 0.01
20 --> 20
10.01 --> 10.01
How can I do this in Swift.
EDIT: Essentially if there are cents (i.e. cents > 0) then display the cents. Otherwise, don't.
Your rule is "Display two fractional digits if either is non-zero; otherwise, display no fractional digits and no decimal point”. I would do it in the most straightforward way:
let number = NSDecimalNumber(string: "12345.00")
let formatter = NSNumberFormatter()
formatter.positiveFormat = "0.00"
let formattedString = formatter.stringFromNumber(number)!
.stringByReplacingOccurrencesOfString(".00", withString: "")
You can use NSNumberFormatter's currency formatting for this. However, there doesn't seem to be a built-in way to do rounding the way you want. Here's a workaround:
let formatter = NSNumberFormatter()
formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
func numToCurrency (num: Double) -> String {
if floor(num) == num {
formatter.minimumFractionDigits = 0
formatter.maximumFractionDigits = 0
}
else {
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 2
}
return formatter.stringFromNumber(num)!
}
numToCurrency(9) // "$9"
numToCurrency(9.9) // "$9.90"
Check the NSNumberFormatter class reference for further configuration options (you might need to set a locale for this formatter to automatically use the correct international currency sign for the current user).
(Answering here, as a closed question was re-directed to this one...)
Perhaps the most straightforward route, particularly since this is tagged "Swift", is to determine if it's a whole number or not:
if value.truncatingRemainder(dividingBy: 1) == 0 {
// it's a whole number,
// so format WITHOUT decimal places, e.g. $12
} else {
// it's a fraction,
// so format WITH decimal places, e.g. $12.25
}
the added benefit is avoiding issues with locales and currency formats... no search/replace of ".00" when you're in Germany, for example, where the format is ",00"
edit/update: Xcode 8.3 • Swift 3.1
extension Formatter {
static let noFractionDigits: NumberFormatter = {
let formatter = NumberFormatter()
formatter.minimumFractionDigits = 0
formatter.maximumFractionDigits = 0
formatter.minimumIntegerDigits = 1
return formatter
}()
static let twoFractionDigits: NumberFormatter = {
let formatter = NumberFormatter()
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 2
formatter.minimumIntegerDigits = 1
return formatter
}()
}
extension FloatingPoint {
var customDescription: String {
return rounded(.down) == self ?
Formatter.noFractionDigits.string(for: self) ?? "" :
Formatter.twoFractionDigits.string(for: self) ?? ""
}
}
extension String {
var double: Double { return Double(self) ?? 0 }
}
let array = ["9.99","10","10.00","9.90","9.9"]
let results = array.map { $0.double.customDescription }
results // ["9.99", "10", "10", "9.90", "9.90"]
Here's how to create a custom formatter class to handle this for you:
import Foundation
class CustomFormatter: NSNumberFormatter {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init() {
super.init()
self.locale = NSLocale.currentLocale()
self.numberStyle = .DecimalStyle
}
func isIntegerNumber(number:NSNumber) -> Bool {
var value: NSDecimal = number.decimalValue
if NSDecimalIsNotANumber(&value) { return false }
var rounded = NSDecimal()
NSDecimalRound(&rounded, &value, 0, NSRoundingMode.RoundPlain)
return NSDecimalCompare(&rounded, &value) == NSComparisonResult.OrderedSame
}
override func stringFromNumber(number: NSNumber) -> String? {
if isIntegerNumber(number) {
self.minimumFractionDigits = 0
self.maximumFractionDigits = 0
return super.stringFromNumber(number)
}
else {
self.minimumFractionDigits = 2
self.maximumFractionDigits = 2
return super.stringFromNumber(number)
}
}
}
let formatter = CustomFormatter()
formatter.stringFromNumber(NSDecimalNumber(double: 5.00)) // -> "5"
formatter.stringFromNumber(NSDecimalNumber(double: 5.01)) // -> "5.01"
formatter.stringFromNumber(NSDecimalNumber(double: 5.10)) // -> "5.10"
Thanks to this post for the proper way to test if a NSDecimal is an integer.
I think it's best to let the currencyStyle determine the maximumFractionDigits. Just set the minimumFractionDigits to 0 where desired. The code is slightly shorter, but as a bonus if you set the locale, this way will allow for languages that don't have 2 decimal places.
Using NSNumberFormatter gives you the benefit of currency symbols, decimal places and comma’s, all in the perfect places for the different locale’s.
extension NSNumber {
func currencyString() -> String? {
let formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
if self.isEqualToNumber(self.integerValue) {
formatter.minimumFractionDigits = 0
}
return formatter.stringFromNumber(self)
}
}
let inputArray: [NSDecimalNumber] = [9.99, 10, 10.00, 9.90, 0, 0.01, 20, 10.01, 0.5, 0.055, 5.0]
let outputArray: [String] = inputArray.map({return $0.currencyString() ?? "nil"})
print(outputArray)
["$9.99", "$10", "$10", "$9.90", "$0", "$0.01", "$20", "$10.01", "$0.50", "$0.06", "$5"]
Adding a locale to a NSNumberFormatter looks like this(ex. from an SKProduct object):
formatter.locale = product!.priceLocale
For an OSX app you need to add:
formatter.formatterBehavior = .Behavior10_4

Resources