I'm trying to take a decimal I'm storing in CoreData and run it through the currency formatter in Swift 3. Here is what I'm trying to use:
var currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = NumberFormatter.Style.currency
// localize to your grouping and decimal separator
currencyFormatter.locale = NSLocale.current
var priceString = currencyFormatter.stringFromNumber(NSNumber(totalAmount))
Where totalAmount is the decimal I'm using for CoreData.
But . I get this error when trying to convert my decimal to a NSNumber()
Argument labels '(_:)' do not match any available overloads
stringFromNumber got renamed to string(from:), e.g.
var priceString = currencyFormatter.string(from: NSNumber(totalAmount))
but you don't have to convert to NSNumber
var priceString = currencyFormatter.string(for: totalAmount)
You can have something like:
class YourClass: UIViewController {
static let priceFormatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.formatterBehavior = .behavior10_4
formatter.numberStyle = .currency
return formatter
}()
}
Usage:
yourLabel.text = YourClass.priceFormatter.string(from: totalAmount)
Related
I am fairly new to Swift and having a great deal of trouble finding a way to add a space as a thousand separator.
What I am hoping to achieve is taking the result of a calculation and displaying it in a textfield so that the format is:
2 358 000
instead of
2358000
for example.
I am not sure if I should be formatting the Int value and then converting it to a String, or adding the space after the Int value is converted to a String. Any help would be greatly appreciated.
You can use NSNumberFormatter to specify a different grouping separator as follow:
update: Xcode 11.5 • Swift 5.2
extension Formatter {
static let withSeparator: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.groupingSeparator = " "
return formatter
}()
}
extension Numeric {
var formattedWithSeparator: String { Formatter.withSeparator.string(for: self) ?? "" }
}
2358000.formattedWithSeparator // "2 358 000"
2358000.99.formattedWithSeparator // "2 358 000.99"
let int = 2358000
let intFormatted = int.formattedWithSeparator // "2 358 000"
let decimal: Decimal = 2358000
let decimalFormatted = decimal.formattedWithSeparator // "2 358 000"
let decimalWithFractionalDigits: Decimal = 2358000.99
let decimalWithFractionalDigitsFormatted = decimalWithFractionalDigits.formattedWithSeparator // "2 358 000.99"
If you need to display your value as currency with current locale or with a fixed locale:
extension Formatter {
static let number = NumberFormatter()
}
extension Locale {
static let englishUS: Locale = .init(identifier: "en_US")
static let frenchFR: Locale = .init(identifier: "fr_FR")
static let portugueseBR: Locale = .init(identifier: "pt_BR")
// ... and so on
}
extension Numeric {
func formatted(with groupingSeparator: String? = nil, style: NumberFormatter.Style, locale: Locale = .current) -> String {
Formatter.number.locale = locale
Formatter.number.numberStyle = style
if let groupingSeparator = groupingSeparator {
Formatter.number.groupingSeparator = groupingSeparator
}
return Formatter.number.string(for: self) ?? ""
}
// Localized
var currency: String { formatted(style: .currency) }
// Fixed locales
var currencyUS: String { formatted(style: .currency, locale: .englishUS) }
var currencyFR: String { formatted(style: .currency, locale: .frenchFR) }
var currencyBR: String { formatted(style: .currency, locale: .portugueseBR) }
// ... and so on
var calculator: String { formatted(groupingSeparator: " ", style: .decimal) }
}
Usage:
1234.99.currency // "$1,234.99"
1234.99.currencyUS // "$1,234.99"
1234.99.currencyFR // "1 234,99 €"
1234.99.currencyBR // "R$ 1.234,99"
1234.99.calculator // "1 234.99"
Note: If you would like to have a space with the same width of a period you can use "\u{2008}"
unicode spaces
formatter.groupingSeparator = "\u{2008}"
You want to use NSNumberFormatter:
let fmt = NSNumberFormatter()
fmt.numberStyle = .DecimalStyle
fmt.stringFromNumber(2358000) // with my locale, "2,358,000"
fmt.locale = NSLocale(localeIdentifier: "fr_FR")
fmt.stringFromNumber(2358000) // "2 358 000"
With Swift 5, when you need to format the display of numbers, NumberFormatter is the right tool.
NumberFormatter has a property called numberStyle. numberStyle can be set to a value of NumberFormatter.Style.decimal in order to set the formatter's style to decimal.
Therefore, in the simplest case when you want to format a number with decimal style, you can use the following Playground code:
import Foundation
let formatter = NumberFormatter()
formatter.numberStyle = NumberFormatter.Style.decimal
let amount = 2358000
let formattedString = formatter.string(for: amount)
print(String(describing: formattedString))
According to the user's current locale, this code will print Optional("2,358,000") for en_US or Optional("2 358 000") for fr_FR.
Note that the following code snippet that uses the NumberFormatter's locale property set to Locale.current is equivalent to the previous Playground code:
import Foundation
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.locale = Locale.current
let amount = 2358000
let formattedString = formatter.string(for: amount)
print(String(describing: formattedString))
The Playground code below that uses the NumberFormatter's groupingSeparator property set to Locale.current.groupingSeparator is also equivalent to the former:
import Foundation
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.groupingSeparator = Locale.current.groupingSeparator
let amount = 2358000
let formattedString = formatter.string(for: amount)
print(String(describing: formattedString))
Otherwise, if you want to set the number formatting with a specific locale formatting style, you may use the following Playground code:
import Foundation
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.locale = Locale(identifier: "fr_FR")
let amount = 2358000
let formattedString = formatter.string(for: amount)
print(String(describing: formattedString))
// prints: Optional("2 358 000")
However, if what you really want is to enforce a specific grouping separator, you may use the Playground code below:
import Foundation
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.groupingSeparator = " "
let amount = 2358000
let formattedString = formatter.string(for: amount)
print(String(describing: formattedString))
// prints: Optional("2 358 000")
Leo Dabus's answer translated to Swift 3:
Into any .swift file, out of a class:
struct Number {
static let withSeparator: NumberFormatter = {
let formatter = NumberFormatter()
formatter.groupingSeparator = " " // or possibly "." / ","
formatter.numberStyle = .decimal
return formatter
}()
}
extension Integer {
var stringWithSepator: String {
return Number.withSeparator.string(from: NSNumber(value: hashValue)) ?? ""
}
}
Usage:
let myInteger = 2358000
let myString = myInteger.stringWithSeparator // "2 358 000"
Code:
//5000000
let formatter = NumberFormatter()
formatter.groupingSeparator = " "
formatter.locale = Locale(identifier: "en_US")
formatter.numberStyle = .decimal.
Output:
5 000 000
I was looking for a currency format like $100,000.00
I accomplished it customizing the implementation Leo Dabus like this
extension Formatter {
static let withSeparator: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.currencyGroupingSeparator = ","
formatter.locale = Locale(identifier: "en_US") //for USA's currency patter
return formatter
}()
}
extension Numeric {
var formattedWithSeparator: String {
return Formatter.withSeparator.string(for: self) ?? ""
}
}
Try this
func addPoints(inputNumber: NSMutableString){
var count: Int = inputNumber.length
while count >= 4 {
count = count - 3
inputNumber.insert(" ", at: count) // you also can use ","
}
print(inputNumber)
}
The call:
addPoints(inputNumber: "123456")
The result:
123 456 (or 123,456)
I am using following code to convert string to US currency. However, I could not figure out how to disable round up.
For example, if the string value is "0.000012" code converts it to "$0.00".
The extension I am using it is from this SO answer:
The way I use:
print(Formatter.currency.locale) // "en_US (current)\n"
print(priceUsdInt.currency) // "$1.99\n"
To control rounding (accept 6 digits after point in this case) add formatter.maximumFractionDigits = 6; to your Formatter extension
extension Formatter {
static let currency = NumberFormatter(style: .currency)
static let currencyUS: NumberFormatter = {
let formatter = NumberFormatter(style: .currency)
formatter.locale = Locale(identifier: "en_US")
formatter.maximumFractionDigits = 6;
return formatter
}()
static let currencyBR: NumberFormatter = {
let formatter = NumberFormatter(style: .currency)
formatter.locale = Locale(identifier: "pt_BR")
formatter.maximumFractionDigits = 6;
return formatter
}()
}
0.000012 is should NOT convert to 0.01 , that would be wrong.
If you want to set up a different format you can change the decimal points
String(format: "%0.3f", string)
I am using iOS charts plugin (line chart) and wish to style the chart values (the number above each point) to a decimal number.
The value is a double, but charts by default is rounding it and displaying it as an integer.
I have tried the following but not working:
let valueformatter = NumberFormatter()
valueformatter.numberStyle = .decimal
valueformatter.locale = Locale.current
lineChartDataSet.valueFormatter = valueformatter as? IValueFormatter
I have tried various other properties but non of them change the format of the number in the dataset.
How can I change the format of the displayed number?
Almost there, just need to add the following class:
//For old Charts version
//class ChartValueFormatter: NSObject, IValueFormatter {
//For Charts version 4.0.1
class ChartValueFormatter: NSObject, ValueFormatter {
fileprivate var numberFormatter: NumberFormatter?
convenience init(numberFormatter: NumberFormatter) {
self.init()
self.numberFormatter = numberFormatter
}
func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
guard let numberFormatter = numberFormatter
else {
return ""
}
return numberFormatter.string(for: value)!
}
}
Now use this as the number formatter:
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.locale = Locale.current
let valuesNumberFormatter = ChartValueFormatter(numberFormatter: numberFormatter)
lineChartDataSet.valueFormatter = valuesNumberFormatter
lineChartDataSet.valueFont = lineChartDataSet.valueFont.withSize(chartFontPointSize)
lineChartDataSet.valueFormatter = DefaultValueFormatter(decimals: 2)
This worked for me:
Swift 5
let valFormatter = NumberFormatter()
valFormatter.numberStyle = .currency
valFormatter.maximumFractionDigits = 2
valFormatter.currencySymbol = "$"
lineChartPrice.leftAxis.valueFormatter = DefaultAxisValueFormatter(formatter: valFormatter)
let data = PieChartData(dataSet: set)
let formatter = NumberFormatter()
formatter.numberStyle = .percent
formatter.maximumFractionDigits = 2
formatter.multiplier = 1.0
formatter.percentSymbol = "%"
formatter.zeroSymbol = ""
data.setValueFormatter(DefaultValueFormatter(formatter: formatter))
I can't figure this out, referencing this post: Struggling with NSNumberFormatter in Swift for currency
I can't get a reference to NSNumberFormatter...i.e. I can't call any methods on formatter? (the below throws an error of expected declaration)
import Foundation
class CurrencyFormatter {
var price = 100
var formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
formatter.stringFromNumber(price)
}
class CurrencyFormatter {
var price = 100
var formatter = NSNumberFormatter()
init() {
formatter.numberStyle = .CurrencyStyle
formatter.stringFromNumber(price)
}
}
I am getting price of an object in string and I want to formate it as currency.
I am using the following code:
var priceString : NSString = urlDict.objectForKey("price") as NSString
var formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
formatter.locale = NSLocale.currentLocale()
let priceNumber = formatter.numberFromString(priceString)
I am getting the data fine in priceString but priceNumber is nil.
And if I try this:
var priceNumber : NSNumber = urlDict.objectForKey("price") as NSNumber
then also the priceNumber is nil.
What approach should I take to achieve a formatted price from the string?
Thanks all for the suggestions:
I have combined above solutions to form a function:
func formatAsPrice(priceString: NSString) -> NSString {
let rsSymbol = "\u{20B9}" // The currency symbol for India
var priceStrTemp = priceString
priceStrTemp = rsSymbol + priceStrTemp
var formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
formatter.locale = NSLocale.currentLocale()
formatter.locale = NSLocale(localeIdentifier: "en_IN") //Explicit
formatter.secondaryGroupingSize = 2 // grouping as Indian currency style
let priceNumber = formatter.numberFromString(priceStrTemp)!
var finalPrice: NSString! = (rsSymbol + " \(priceNumber)") as NSString!
return finalPrice
}
The Formatter code is fine.
You have to pass correct Currency string with currency symbol
func format(amount: String) -> NSNumber? {
let formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
formatter.locale = NSLocale.currentLocale()
return formatter.numberFromString(amount)
}
format("NOK 12,00") // returns 12.00
format("NOK 12") // Also fine
format("$ 12") // Error, my local currency is NOK (Norwegian krone)
format("12,00") // Error, needs currency symbol in the string.
If you want to parse trying without currency sublime like this "12,00" that you should set currencySymbol to empty string. Like this
func format(amount: String) -> NSNumber? {
let formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
formatter.currencySymbol = ""
...
}
format("12,00") // Works fine now