回答編集履歴
1
extensionを書いてみた
test
CHANGED
@@ -34,12 +34,48 @@
|
|
34
34
|
|
35
35
|
```
|
36
36
|
|
37
|
+
#別解
|
38
|
+
|
39
|
+
```Swift
|
40
|
+
|
41
|
+
extension String {
|
42
|
+
|
43
|
+
func suffixZeroSuppress() -> String? {
|
44
|
+
|
45
|
+
guard let d = Double(self) else {
|
46
|
+
|
47
|
+
return nil
|
48
|
+
|
49
|
+
}
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
var t = String(d)
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
if let range = t.range(of: ".0") {
|
58
|
+
|
59
|
+
t.replaceSubrange(range, with: "")
|
60
|
+
|
61
|
+
}
|
62
|
+
|
63
|
+
return t
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
}
|
37
68
|
|
38
69
|
|
39
70
|
|
71
|
+
print("12.34000".suffixZeroSuppress()!) //=> 12.34
|
40
72
|
|
41
|
-
|
73
|
+
print("-12.00".suffixZeroSuppress()!) // => -12
|
42
74
|
|
75
|
+
print("0.00".suffixZeroSuppress()!) // => 0
|
43
76
|
|
77
|
+
print("000.0000".suffixZeroSuppress()!) // => 0
|
44
78
|
|
45
|
-
|
79
|
+
print("0000".suffixZeroSuppress()!) // => 0
|
80
|
+
|
81
|
+
```
|