Skip to content

Commit 1e1bda9

Browse files
author
Eric Wu
committed
增加保留几位小数接口
1 parent fdff9df commit 1e1bda9

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

CRBoostSwift/Classes/Foundation+CRBoost.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,22 @@ extension String {
124124
return result
125125
}
126126

127+
@discardableResult
128+
public func retainDecimal(_ digits: Int) -> String {
129+
let formatter = NumberFormatter()
130+
formatter.numberStyle = .decimal
131+
formatter.minimumFractionDigits = digits
132+
formatter.usesGroupingSeparator = false
133+
var decimal = NSDecimalNumber(string: isEmpty ? "0" : self)
134+
if decimal.decimalValue == .nan {
135+
decimal = NSDecimalNumber(string: "0")
136+
}
137+
if let value = formatter.string(for: decimal) {
138+
return value
139+
}
140+
return "0"
141+
}
142+
127143
@discardableResult
128144
public func randomStringLength(len: Int) -> String {
129145
let randomCharacters = (0 ..< len).map { _ in self.randomElement()! }
@@ -751,7 +767,7 @@ extension UIImage {
751767
let context = CIContext(options: nil)
752768
if let bitmapImage = context.createCGImage(output, from: output.extent) {
753769
// 4. 显示二维码
754-
let newImage = UIImage(cgImage: bitmapImage, scale: UIScreen.main.scale, orientation: UIImage.Orientation.up)
770+
let newImage = UIImage(cgImage: bitmapImage, scale: UIScreen.main.scale, orientation: UIImage.Orientation.up)
755771
return newImage
756772
}
757773
}

Example/CRBoostSwift/ViewController.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ class ViewController: UIViewController {
1919
}
2020

2121
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
22-
let rect = CGRect(x: 25, y: 20, width: 100, height: 100)
23-
print("midX\(rect.midX),midY:\(rect.midY)")
24-
print(CRFrameCenter(rect: rect))
25-
btnAdd.centerX = view.centerX
26-
btnAdd.centerY = view.centerY
27-
print(CRIdfa)
22+
// print("a".retainDecimal(5))
23+
// print("1".retainDecimal(5))
24+
// print(".11".retainDecimal(5))
25+
// print(".".retainDecimal(5))
26+
print("".retainDecimal(5))
2827

2928
}
3029
}

0 commit comments

Comments
 (0)