質問編集履歴
1
メモ
test
CHANGED
File without changes
|
test
CHANGED
@@ -15,3 +15,53 @@
|
|
15
15
|
print(text) // => A: 4.26 B: 12.81
|
16
16
|
|
17
17
|
```
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
---
|
24
|
+
|
25
|
+
メモ
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
```swift
|
30
|
+
|
31
|
+
extension NSDecimalNumber {
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
var percentageStrWithTwoFractionDigits: String {
|
36
|
+
|
37
|
+
let nf = NumberFormatter()
|
38
|
+
|
39
|
+
nf.minimumFractionDigits = 2
|
40
|
+
|
41
|
+
nf.maximumFractionDigits = 2
|
42
|
+
|
43
|
+
nf.numberStyle = .percent
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
return nf.string(for: self) ?? ""
|
48
|
+
|
49
|
+
}
|
50
|
+
|
51
|
+
}
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
let a: String = "2.2"
|
56
|
+
|
57
|
+
let b: String = "6.6"
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
let v = NSDecimalNumber(string: a).dividing(by: NSDecimalNumber(string: b)).percentageStrWithTwoFractionDigits
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
print(v) //=> 33.33%
|
66
|
+
|
67
|
+
```
|