Skip to content

Commit

Permalink
format currency according to the locale - use NSNumberFormatter to au…
Browse files Browse the repository at this point in the history
…tomatically handle currency symbol, thousands/decimal separator

not reformatting when switching out/back into the app, only when view reloads
  • Loading branch information
jontsai committed Aug 29, 2014
1 parent 72171ef commit db224f8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
3 changes: 1 addition & 2 deletions tips/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6206.8" systemVersion="13E28" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" initialViewController="7ip-7N-UBI">
<dependencies>
<deployment defaultVersion="1792" identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7026.1"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -146,7 +145,7 @@
<nil key="highlightedColor"/>
</label>
<view contentMode="scaleToFill" id="fdb-zN-cI6">
<rect key="frame" x="2" y="271" width="316" height="1"/>
<rect key="frame" x="0.0" y="271" width="320" height="1"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" white="0.33333333333333331" alpha="1" colorSpace="calibratedWhite"/>
</view>
Expand Down
8 changes: 4 additions & 4 deletions tips/TipsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class TipsViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tipLabel.text = "$0.00"
totalLabel.text = "$0.00"
tipLabel.text = formatCurrency(0.0)
totalLabel.text = formatCurrency(0.0)
}

override func viewWillAppear(animated: Bool) {
Expand Down Expand Up @@ -71,8 +71,8 @@ class TipsViewController: UIViewController {
if (wasEdited) {
storeLastBillAmount(billAmount)
}
tipLabel.text = String(format: "$%.2f", tip)
totalLabel.text = String(format: "$%.2f", total)
tipLabel.text = formatCurrency(tip)
totalLabel.text = formatCurrency(total)
}

}
Expand Down
7 changes: 7 additions & 0 deletions tips/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ func getDefaults(key: String) -> AnyObject? {
var value: AnyObject? = defaults.objectForKey(key)
return value
}

func formatCurrency(amount: Double) -> String {
var numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
var formattedAmount = numberFormatter.stringFromNumber(amount)
return formattedAmount
}

0 comments on commit db224f8

Please sign in to comment.