Home > Back-end >  Transform an integer to a currency format in SwiftUI
Transform an integer to a currency format in SwiftUI

Time:07-19

How can I convert an Int to display a dollar amount? The sets of numbers I have do not have a decimal point such 1234.45

Therefore, how can I transform the Int 175533000 to $175,533,000 but not $175,533,000.00?

CodePudding user response:

A possible way is the extremely versatile formatted API introduced in iOS 15 / macOS 12

let value = 175533000
let formatted = value.formatted(.currency(code: "usd")
    .precision(.fractionLength(0))
    .locale(.init(identifier: "en_US")))
print(formatted) 

CodePudding user response:

  • Related