回答編集履歴
1
extensionを書いてみた
answer
CHANGED
|
@@ -16,8 +16,26 @@
|
|
|
16
16
|
return t
|
|
17
17
|
}
|
|
18
18
|
```
|
|
19
|
+
#別解
|
|
20
|
+
```Swift
|
|
21
|
+
extension String {
|
|
22
|
+
func suffixZeroSuppress() -> String? {
|
|
23
|
+
guard let d = Double(self) else {
|
|
24
|
+
return nil
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var t = String(d)
|
|
28
|
+
|
|
29
|
+
if let range = t.range(of: ".0") {
|
|
30
|
+
t.replaceSubrange(range, with: "")
|
|
31
|
+
}
|
|
32
|
+
return t
|
|
33
|
+
}
|
|
34
|
+
}
|
|
19
35
|
|
|
20
|
-
|
|
36
|
+
print("12.34000".suffixZeroSuppress()!) //=> 12.34
|
|
21
|
-
|
|
37
|
+
print("-12.00".suffixZeroSuppress()!) // => -12
|
|
22
|
-
|
|
23
|
-
|
|
38
|
+
print("0.00".suffixZeroSuppress()!) // => 0
|
|
39
|
+
print("000.0000".suffixZeroSuppress()!) // => 0
|
|
40
|
+
print("0000".suffixZeroSuppress()!) // => 0
|
|
41
|
+
```
|