I am trying to convert a double to a string in currency format.

Hello,


How do I format a double into s string currency format?

I can convert a double to a string but can't format it into U.S. currency. I am trying to print the monthly payment in currency format.

This is what I did. I can put numbers into NSNumber but I can't the output there. dblMonthlyPayment is my variable.

I tried:


String(dblMonthlyPayment)


1. class CurrencyFormatter: NumberFormatter {

2. required init?(coder aDecoder: NSCoder) {

3. super.init(coder: aDecoder)

4. }

5. override init() {

6. super.init()

7. self.locale = NSLocale.current

8. self.maximumFractionDigits = 2

9. self.minimumFractionDigits = 2

10. self.alwaysShowsDecimalSeparator = true

11. self.numberStyle = .currency

12. }

13. static let SharedInstance = CurrencyFormatter()

14.

15. }

16. // Print the Monthly Payment.

17. lblMonthlyPayment.text = CurrencyFormatter.SharedInstance.string(from: NSNumber)

What sort of output do you want? Just saying U.S. currency and showing a code (which is supposed not to work as you expect) is not sufficient.


Please show by examples.

(And please do not keep similar questions alone. You can delete or edit your posts.)

Your code is way more complex than it needs to be. If you want to render a currency in the user’s current locale, you can do that with one call to a

NumberFormatter
convenience method:
import Foundation

let result = NumberFormatter.localizedString(from: 9999.99, number: .currency)
print(result)   // -> £9,999.99

This prints in GBP because my locale is

en_GB
. If you want to override the locale, and thus render in a specific currency, you’ll need an instance of
NumberFormatter
(note that you don’t need to subclass it). There’s actually two ways to do this, and the best approach depends on your specific context.
  • If you want to format the number in a ‘foreign’ currency in the user’s locale, you do this:

    import Foundation
    
    
    let formatter = NumberFormatter()
    formatter.numberStyle = .currency
    formatter.currencyCode = "USD"
    let result = formatter.string(from: 9999.99)!
    print(result)   // -> US$9,999.99

    Note that this uses

    US$
    because to British users a plain
    $
    is ambiguous (it could be USD, AUD, CAD, or whatever).
  • If you want to format the number as if it were in another user’s locale, you do this:

    import Foundation
    
    
    let formatter = NumberFormatter()
    formatter.numberStyle = .currency
    formatter.locale = Locale(identifier: "en_US")
    let result = formatter.string(from: 9999.99)!
    print(result)   // -> $9,999.99

    Here you get

    $
    because that’s what a US user would see.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you for the reply.


I understand if I want the output to read $9999.99 then I put in that but how do I put the value stored in a double?

Accepted Answer

In Swift, you can often read the method name to figure out how to use it. In Quinn's example:


let result = NumberFormatter.localizedString(from: 9999.99, number: .currency)


This reads, "ask the number formatter for a localized string from the number 9999.99 with the currency number format." So, in your scenario, replace the 9999.99 with an NSNumber object. Here's how you create an NSNumber object from a Double variable:


NSNumber(value: variable)


This reads, "create an NSNumber object with the value in 'variable'."

Thank you!


It took me awhile but I finally figured it out.

Can you help me with something else please? I am almost done with my app.

How do you format a text field to U.S. currency as the user is typing? I tried the same thing but I put the code under the btnCalculate function so it only formatted it after the calculate button is clicked.


Thank you

Here's an example of how to do that: https://stackoverflow.com/a/35845040

I am trying to convert a double to a string in currency format.
 
 
Q