The output of following Code is: ARS 59.00. Why is there no symbol printed?
let formatter = NumberFormatter()
formatter.currencyCode = "ARS"
formatter.numberStyle = .currency
print(formatter.string(from: NSNumber(value: 59.00)) ?? "na")
Forcing a Custom Locale
To display currency, you will need to show the currency symbol ($, €, ¥, £) for the current locale.
NumberFormatter will show the correct symbol, and the formatting that you might not realize is very different from what you're used to. Different countries use different decimal separators and grouping separators—take a look!
In the USA: $3,490,000.89
In France: 3 490 000,89 €
In Germany: 3.490.000,89 €
See: How to Use NumberFormatter (NSNumberFormatter) in Swift to Make Currency Numbers Easy to Read
So in your case it would be:
let formatter = NumberFormatter()
formatter.currencyCode = "ARS"
formatter.numberStyle = .currency
formatter.locale = Locale(identifier: "es_AR")
print(formatter.string(from: NSNumber(value: 59.00)) ?? "na")
Try this:
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = Locale(identifier: "es_AR")
print(formatter.string(from: NSNumber(value: 59.00)) ?? "na")
check the local identifier here: https://gist.github.com/jacobbubu/1836273
Related
In the Hebrew calendar, the days in the month are text.
For example, the first in "Sivan" (name of a month) is written "א" in Hebrew (and not "1" like in the Gregorian calendar).
This is my code:
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .hebrew)
formatter.dateFormat = "d"
print(formatter.string(from: date)) // "1"
But unfortunately, instead of getting א I'm getting 1.
I wanted to know if there's a way to get it as it should have been? Thanks
I initially thought it was just a matter of setting Locale on the DateFormatter, but turns out a date style was also needed to be set:
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .hebrew)
formatter.locale = Locale(identifier: "he") // <- this
formatter.dateStyle = .short // <- and this
formatter.dateFormat = "dd" // after dateStyle
print(formatter.string(from: Date())) // כ״ט
How to convert an Array of NSNumber to Array of String in order to display the value in UITableView?
cell.textlabel.text = ?
Code:
var a = [68.208983, 6.373902, 1.34085, 3.974012, 110.484001,
61.380001, 1.325202, 0.8501030000000001, 0.8501030000000001,
0.8501030000000001, 3.647296, 1.28503]
just add
.stringValue
to your NSNumber variable
From what you posted is an array of Double if you don't annotate them explicitly. If the array you posted is as it is, then you need this:
let arrayOfDoubles = [68.208983, 6.373902, 1.34085, 3.974012, 110.484001, 61.380001, 1.325202, 0.8501030000000001, 0.8501030000000001, 0.8501030000000001, 3.647296, 1.28503]
let stringArrayOfDoubles = arrayOfDoubles.map { String($0) }
Or, if you explicitly annotate the type as [NSNumber] then you will need this:
let arrayOfNumbers: [NSNumber] = [68.208983, 6.373902, 1.34085, 3.974012, 110.484001, 61.380001, 1.325202, 0.8501030000000001, 0.8501030000000001, 0.8501030000000001, 3.647296, 1.28503]
let stringArrayOfNumbers = arrayOfNumbers.map { $0.stringValue }
If you're going to display numeric data to the user, it's best to use a number formatter. That way your output will adapt to the formatting that your users are used to and expect based on their locale. It also allows you to configure how the numbers are presented (number of fraction digits, significant digits, rounding, etc.) without having to modify the numbers. For example, if you want to format a number as a decimal with two fraction digits, you would configure the formatter like this:
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 2
Depending on the user's locale, the output (thousand separator, decimal separator, and even digits(!)) will vary:
formatter.locale = Locale(identifier: "en")
formatter.string(from: 12345.6789) // 12,345.68
formatter.string(from: 0.12345) // 0.12
formatter.locale = Locale(identifier: "sv")
formatter.string(from: 12345.6789) // 12 345,68
formatter.string(from: 0.12345) // 0,12
formatter.locale = Locale(identifier: "hi")
formatter.string(from: 12345.6789) // १२,३४५.६८
formatter.string(from: 0.12345) // ०.१२
formatter.locale = Locale(identifier: "ar")
formatter.string(from: 12345.6789) // ١٢٬٣٤٥٫٦٨
formatter.string(from: 0.12345) // ٠٫١٢
Other number styles can be used to format other types of numeric data like currency, percent, or ordinal numbers:
formatter.locale = Locale(identifier: "en")
formatter.numberStyle = .ordinal
formatter.string(from: 1) // 1st
formatter.string(from: 2) // 2nd
formatter.string(from: 3) // 3rd
formatter.string(from: 4) // 4th
Convert givent value in to String.
If given value is nil then it will return empty string.
class func toString(_ anything: Any?) -> String {
if let any = anything {
if let num = any as? NSNumber {
return num.stringValue
} else if let str = any as? String {
return str
}
}
return ""
}
Just Copy paste this method convert to string without crash issue
Thanks.
I try to convert string "0.0004131955" to decimal/double/currency with NumberFormatter. It produces nil.
How to convert it?
My sample code is as below:
let formatter = NumberFormatter()
formatter.locale = Locale.current
formatter.numberStyle = .currency
formatter.minimumFractionDigits = 1
formatter.maximumFractionDigits = 15
formatter.usesGroupingSeparator = true
//number is nil here
if let number = formatter.number(from: "0.0004131955") {
let unitPrice = number.doubleValue
}
What am I doing wrong?
Remove the currency from numberStyle - copy to a playground to test
import UIKit
import PlaygroundSupport
let formatter = NumberFormatter()
formatter.locale = Locale.current
//formatter.numberStyle = .currency
formatter.minimumFractionDigits = 1
formatter.maximumFractionDigits = 15
formatter.usesGroupingSeparator = true
let number: NSNumber? = formatter.number(from: "0.0004131955")
let unitPrice: Double? = number?.doubleValue
HI #erdemgc for Number stlye Deciamal your code is working fine for me you can check console in below image
and for the currency you have to specify a currency style format you for Example i am attaching a image please take a look of below description
I have a string, "$4,102.33" that needs to be converted to double. This will always be US. My only way is a hack to strip out the $ and , and then convert to double. Seems like NSFormatter only lets me convert TO a currency and not from it. Is there a built-in function or better way than just removing the $ and ,? prior to converting it to double?
NumberFormatter can convert to and from string. Also Double cannot represent certain numbers exactly since it's based 2. Using Decimal is slower but safer.
let str = "$4,102.33"
let formatter = NumberFormatter()
formatter.numberStyle = .currency
if let number = formatter.number(from: str) {
let amount = number.decimalValue
print(amount)
}
To convert from String to NSNumber for a given currency is easy:
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = Locale(identifier: "en_US")
let number = formatter.number(from: string)
To get your number as a Double or as a Decimal (preferred) is then direct:
let doubleValue = number?.doubleValue
let decimalValue = number?.decimalValue
Is there a simple command to format 1.60543e+06 to 1,605,436???
resultFV.text = String.localizedStringWithFormat("%f", fv)
does not get it.
In Swift 3,
NumberFormatter.localizedString(from: NSNumber(value: whatever), number: NumberFormatter.Style.decimal)
Swift Xcode 6.3, SOLVED (I decided to leave the $ in the code). If you don't want a $ in the output, change .CurrencyStyle to .DecimalStyle
var fv = 3534234.55
var formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
formatter.maximumFractionDigits = 0;
resultFV.text = formatter.stringFromNumber(fv) // result: $3,534,235 –
You should use a NumberFormatter for that:
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
resultFV.text = numberFormatter.string(from: fv)
Update for Swift 4.1 currency string:
let balance = 1234567
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.maximumFractionDigits = 0
let result = formatter.string(from: NSNumber(value: balance))
// result: $1,234,567
You can change formatter.numberStyle to .decimal to leave it as number without "$" sign.
You can achieve this by using String initializers in Swift 3+:
// 1605436
let value: Int = 1605436
// "1,605,436" where Locale == en_US
let formattedInt = String(format: "%d", locale: Locale.current, value)
// "1,605,436" where Locale == en_US
let formattedDouble = String(format: "%.0f", locale: Locale.current, Double(value))
Update for Swift 5
var unformattedValue : Double = 3534234.55
var formatter = NumberFormatter()
formatter.numberStyle = .currency // or .decimal if desired
formatter.maximumFractionDigits = 2; //change as desired
formatter.locale = Locale.current // or = Locale(identifier: "de_DE"), more locale identifier codes: https://gist.github.com/jacobbubu/1836273
var displayValue : String = formatter.string(from: NSNumber(value: unformattedValue))! // displayValue: "$3,534,235" ```
Why don't you limit the precision, like ".0f"
resultFV.text = String.localizedStringWithFormat("%.0f", fv)
Updated Answer:
var formatter: NSNumberFormatter = NSNumberFormatter()
formatter.numberStyle = NSNumberFormatterStyle.DecimalStyle;
var formattedStr: NSString = formatter.stringFromNumber(NSNumber(double: fv))!
resultFV.text = formattedStr
Updated
Using Data Formatting available in Foundation for macOS 12.0+, iOS 15.0+, tvOS 15.0+, and watchOS 8.0+.
let number: Int = 1000
let formatted = number.formatted(.number.grouping(.never))
print(formatted)
console output: "1000"
number can be any BinaryFloatingPoint or BinaryInteger