0Day Forums
Swift how to format a large number with thousands separators? - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: Swift (https://0day.red/Forum-Swift)
+--- Thread: Swift how to format a large number with thousands separators? (/Thread-Swift-how-to-format-a-large-number-with-thousands-separators)



Swift how to format a large number with thousands separators? - Mranniswchhfzfg - 07-18-2023

Is there a simple command to format 1.60543e+06 to 1,605,436???

resultFV.text = String.localizedStringWithFormat("%f", fv)

does not get it.



RE: Swift how to format a large number with thousands separators? - cirrus471 - 07-19-2023

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`



RE: Swift how to format a large number with thousands separators? - deciare654054 - 07-19-2023

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:
var displayValue : String = formatter.string(from: NSNumber(value: unformattedValue))! // displayValue: "$3,534,235" ```


RE: Swift how to format a large number with thousands separators? - naphthylic31585 - 07-19-2023

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))


RE: Swift how to format a large number with thousands separators? - radoslavlsalgjgsfx - 07-19-2023

You should use a `NumberFormatter` for that:

let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal

resultFV.text = numberFormatter.string(from: fv)


RE: Swift how to format a large number with thousands separators? - jazzman799498 - 07-19-2023

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.



RE: Swift how to format a large number with thousands separators? - Mrsisterhoods409 - 07-19-2023

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 –




RE: Swift how to format a large number with thousands separators? - boulder964 - 07-19-2023

In Swift 3,

NumberFormatter.localizedString(from: NSNumber(value: whatever), number: NumberFormatter.Style.decimal)


RE: Swift how to format a large number with thousands separators? - fushih318 - 07-19-2023

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