Browse Source

【综合】接口参数保留两位小数

tangchao 4 months ago
parent
commit
c0ab0ffd23

+ 9 - 0
PDF Office/PDF Master/Class/Common/KMCommonDefine.swift

@@ -90,6 +90,15 @@ public func KMMainThreadExecute(_ execute: @escaping (()->Void)) {
     }
 }
 
+public func KMFormatNSNumber(_ number: NSNumber, to n: Int) -> String? {
+    let formatter = NumberFormatter()
+    formatter.minimumIntegerDigits = 1
+    formatter.maximumIntegerDigits = 20 // or however many digits you need
+    formatter.minimumFractionDigits = n
+    formatter.maximumFractionDigits = n
+    return formatter.string(from: number)
+}
+
 // ColumnID
 
 let kPageColumnId = NSUserInterfaceItemIdentifier(rawValue: "page")

+ 60 - 76
PDF Office/PDF Master/MemberCenter/Model/KMMemberCenterManager.swift

@@ -975,40 +975,34 @@ class KMMemberCenterManager: NSObject {
                     if let token = dict1["cycle"] { cycle = token as? Int ?? cycle }
                     if let token = dict1["code"] { code = token as? String ?? code  }
                     
-                    if let token = dict1["price"] {
-                        if let tPrice = token as? NSNumber {
-                            priceString = "\(tPrice)"
-                            price = tPrice
-                        }
-
+                    if let token = dict1["price"] as? NSNumber {
+                        let string = KMFormatNSNumber(token, to: 2)
+                        priceString = string
+                        price = token
                     }
                     
-                    if let token = dict1["displayPrice"] {
-                        if let tPrice = token as? NSNumber {
-                            displayPriceString = "\(tPrice)"
-                            displayPrice = tPrice
-                        }
+                    if let token = dict1["displayPrice"] as? NSNumber {
+                        let string = KMFormatNSNumber(token, to: 2)
+                        displayPriceString = string
+                        displayPrice = token
                     }
                     
-                    if let token = dict1["upgradePrice"] {
-                        if let tPrice = token as? NSNumber {
-                            upgradePriceString = "\(tPrice)"
-                            upgradePrice = tPrice
-                        }
+                    if let token = dict1["upgradePrice"] as? NSNumber {
+                        let string = KMFormatNSNumber(token, to: 2)
+                        upgradePriceString = string
+                        upgradePrice = token
                     }
                     
-                    if let token = dict1["cnyPrice"] {
-                        if let tPrice = token as? NSNumber {
-                            cnyPriceString = "\(tPrice)"
-                            cnyPrice = tPrice
-                        }
+                    if let token = dict1["cnyPrice"] as? NSNumber {
+                        let string = KMFormatNSNumber(token, to: 2)
+                        cnyPriceString = string
+                        cnyPrice = token
                     }
                     
-                    if let token = dict1["displayCnyPrice"] {
-                        if let tPrice = token as? NSNumber {
-                            displayCnyPriceString = "\(tPrice)"
-                            displayCnyPrice = tPrice
-                        }
+                    if let token = dict1["displayCnyPrice"] as? NSNumber {
+                        let string = KMFormatNSNumber(token, to: 2)
+                        displayCnyPriceString = string
+                        displayCnyPrice = token
                     }
 
                     let products = KMListingProductsModel(id: id, productName: productName, price: priceString, maxDeviceNum: maxDeviceNum, displayPrice: displayPriceString, levels: levels, platforms: platforms, productLineId: productLineId, paymentModel: paymentModel, cycle: cycle, cnyPrice: cnyPriceString, displayCnyPrice: displayCnyPriceString, upgradePrice: upgradePriceString, code: code)
@@ -1096,35 +1090,30 @@ class KMMemberCenterManager: NSObject {
                     if let token = dict1["cycle"] { cycle = token as? Int ?? cycle }
                     if let token = dict1["code"] { code = token as? String ?? code  }
                     
-                    if let token = dict1["price"] {
-                        if let tPrice = token as? NSNumber {
-                            priceString = "\(tPrice)"
-                            price = tPrice
-                        }
+                    if let token = dict1["price"] as? NSNumber {
+                        let string = KMFormatNSNumber(token, to: 2)
+                        priceString = string
+                        price = token
                     }
-                    if let token = dict1["displayPrice"] {
-                        if let tPrice = token as? NSNumber {
-                            displayPriceString = "\(tPrice)"
-                            displayPrice = tPrice
-                        }
+                    if let token = dict1["displayPrice"] as? NSNumber {
+                        let string = KMFormatNSNumber(token, to: 2)
+                        displayPriceString = string
+                        displayPrice = token
                     }
-                    if let token = dict1["upgradePrice"] {
-                        if let tPrice = token as? NSNumber {
-                            upgradePriceString = "\(tPrice)"
-                            upgradePrice = tPrice
-                        }
+                    if let token = dict1["upgradePrice"] as? NSNumber {
+                        let string = KMFormatNSNumber(token, to: 2)
+                        upgradePriceString = string
+                        upgradePrice = token
                     }
-                    if let token = dict1["cnyPrice"] {
-                        if let tPrice = token as? NSNumber {
-                            cnyPriceString = "\(tPrice)"
-                            cnyPrice = tPrice
-                        }
+                    if let token = dict1["cnyPrice"] as? NSNumber {
+                        let string = KMFormatNSNumber(token, to: 2)
+                        cnyPriceString = string
+                        cnyPrice = token
                     }
-                    if let token = dict1["displayCnyPrice"] {
-                        if let tPrice = token as? NSNumber {
-                            displayCnyPriceString = "\(tPrice)"
-                            displayCnyPrice = tPrice
-                        }
+                    if let token = dict1["displayCnyPrice"] as? NSNumber {
+                        let string = KMFormatNSNumber(token, to: 2)
+                        displayCnyPriceString = string
+                        displayCnyPrice = token
                     }
 
                     let products = KMListingProductsModel(id: id, productName: productName, price: priceString, maxDeviceNum: maxDeviceNum, displayPrice: displayPriceString, levels: levels, platforms: platforms, productLineId: productLineId, paymentModel: paymentModel, cycle: cycle, cnyPrice: cnyPriceString, displayCnyPrice: displayCnyPriceString, upgradePrice: upgradePriceString, code: code)
@@ -1278,35 +1267,30 @@ class KMMemberCenterManager: NSObject {
                 if let token = result["cycle"] { cycle = token as? Int ?? cycle }
                 if let token = result["code"] { code = token as? String ?? code  }
                 
-                if let token = result["price"] {
-                    if let tPrice = token as? NSNumber {
-                        priceString = "\(tPrice)"
-                        price = tPrice
-                    }
+                if let token = result["price"] as? NSNumber {
+                    let string = KMFormatNSNumber(token, to: 2)
+                    priceString = string
+                    price = token
                 }
-                if let token = result["displayPrice"] {
-                    if let tPrice = token as? NSNumber {
-                        displayPriceString = "\(tPrice)"
-                        displayPrice = tPrice
-                    }
+                if let token = result["displayPrice"] as? NSNumber {
+                    let string = KMFormatNSNumber(token, to: 2)
+                    displayPriceString = string
+                    displayPrice = token
                 }
-                if let token = result["upgradePrice"] {
-                    if let tPrice = token as? NSNumber {
-                        upgradePriceString = "\(tPrice)"
-                        upgradePrice = tPrice
-                    }
+                if let token = result["upgradePrice"] as? NSNumber {
+                    let string = KMFormatNSNumber(token, to: 2)
+                    upgradePriceString = string
+                    upgradePrice = token
                 }
-                if let token = result["cnyPrice"] {
-                    if let tPrice = token as? NSNumber {
-                        cnyPriceString = "\(tPrice)"
-                        cnyPrice = tPrice
-                    }
+                if let token = result["cnyPrice"] as? NSNumber {
+                    let string = KMFormatNSNumber(token, to: 2)
+                    cnyPriceString = string
+                    cnyPrice = token
                 }
-                if let token = result["displayCnyPrice"] {
-                    if let tPrice = token as? NSNumber {
-                        displayCnyPriceString = "\(tPrice)"
-                        displayCnyPrice = tPrice
-                    }
+                if let token = result["displayCnyPrice"] as? NSNumber {
+                    let string = KMFormatNSNumber(token, to: 2)
+                    displayCnyPriceString = string
+                    displayCnyPrice = token
                 }
 
                 let products = KMCheckCouponModel(id: id, productName: productName, price: priceString, maxDeviceNum: maxDeviceNum, displayPrice: displayPriceString, levels: levels, platforms: platforms, productLineId: productLineId, paymentModel: paymentModel, cycle: cycle, cnyPrice: cnyPriceString, displayCnyPrice: displayCnyPriceString, upgradePrice: upgradePriceString, code: code)