Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 546 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Swift how to format a large number with thousands separators?

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

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

does not get it.
Reply

#2
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`
Reply

#3
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" ```
Reply

#4
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))
Reply

#5
You should use a `NumberFormatter` for that:

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

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

#6
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.
Reply

#7
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 –

Reply

#8
In Swift 3,

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

#9
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
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through