How to convert Decimal to String with two digits after separator?

前端 未结 2 1654
囚心锁ツ
囚心锁ツ 2021-01-16 04:46

This is what I do now:

extension Decimal {
    var formattedAmount: String {
        let formatter = NumberFormatter()
        formatter.generatesDecimalNumbers =         


        
2条回答
  •  礼貌的吻别
    2021-01-16 04:59

    This:

    formatter.string(from: ...) // requires a NSNumber
    

    You can either do this:

    formatter.string(for: self) // which takes Any as argument
    

    Or:

    string(from: self as NSDecimalNumber) // cast self to NSDecimalNumber
    

提交回复
热议问题