String 与 Int、Double 互相转换的方法及 Error Condition Test

整理了一下上一个博客,可能之前写的 String 与 Int、Double 互相转换的方法还有用,就搬到这边来了,重新排了个版。

String 转换成 Double:

  1. CoreFoundation -> CFString
  2. String 和 NSString 有着良好的互相转换的特性
  3. NSNumberFormatter
  4. Darwin -> C -> stdlib (Apple Public Source License)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// String to Double
var str:String = "4.19"

// CoreFoundation -> CFString
CFStringGetDoubleValue(str)

/* Parse from left;
eg. "a4.19" -> 0.0; "4.19a" -> 4.19
Skips whitespace of begining and ending;
eg. "4 .19" -> 4.0; "4. 19" -> 4.0; "4.1 9" -> 4.1;
returns 0.0 on error */

// NSString
NSString(string: str).doubleValue
(str as NSString).doubleValue

/* Parse from left;
eg. "a4.19" -> 0.0; "4.19a" -> 4.19
Skips whitespace of begining and ending;
eg. "4 .19" -> 4.0; "4. 19" -> 4.0; "4.1 9" -> 4.1;
returns 0.0 on error */

// NSNumberFormatter
NSNumberFormatter().numberFromString(str)?.doubleValue

/* Skips whitespace of begining and ending;
eg. " 4.19 " -> 4.19; "4. 19" -> nil; "4.1 9" -> nil;
returns nil on error */

// Darwin -> C -> stdlib
atof(str)

/* Parse from left;
eg. "a4.19" -> 0.0; "4.19a" -> 4.19
Skips whitespace of begining and ending;
eg. "4 .19" -> 4.0; "4. 19" -> 4.0; "4.1 9" -> 4.1;
returns 0.0 on error */

String 转换成 Float:

  1. NSNumberFormatter
1
2
3
4
5
6
7
8
9
// String to Float

// NSNumberFormatter
NSNumberFormatter().numberFromString("2.3")?.floatValue
// 结果:Some(2.29999995231628)

/* Float会丢失精度,如需要准确的数字使用Double;
NSNumberFormatter().numberFromString("test")?.floatValue -> nil
*/

String 转换成 Int:

  1. Darwin -> C -> stdlib (Apple Public Source License)
  2. NSString
  3. NSNumberFormatter
  4. CoreFoundation -> CFString
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// String to Int

// Darwin -> C -> stdlib
atoi("141") // Int32
atol("343") // Int

/* atoi("141.8") -> 141; atoi("a141.8") -> 0; atoi("14a1.8") -> 14; atoi("14 1.8") ->14;
returns 0 on error*/

// NSString
("6" as NSString).integerValue

/* Parse from left;
eg. "a4" -> 0; "4a" -> 4
Skips whitespace of begining and ending;
eg. "4 19" -> 4; "41 9" -> 41;
returns 0 on error */

// NSNumberFormatter
NSNumberFormatter().numberFromString("3")?.integerValue

/* Skips whitespace of begining and ending;
eg. "4 19" -> nil; " 419" -> 419;
"3.7" -> 3;
returns nil on error */

// CoreFoundation -> CFString
CFStringGetIntValue("6")

/* Parse from left;
eg. "a4" -> 0; "4a" -> 4
Skips whitespace of begining and ending;
eg. "4 19" -> 4; "41 9" -> 41;
"3.7" -> 3;
returns 0 on error */

Int 转换成 String:

1
2
3
4
5
6
7
// Int to String
var integer:Int = 6

// String
"\(integer)"
String(integer)
String.convertFromStringInterpolationSegment(integer)

Float 转换成 String:

1
2
3
4
5
6
7
8
9
// Float to String
var flow:Float = 4.19

// String
"\(flow)"
String.convertFromStringInterpolationSegment(flow)

/* String(4.19) -> String doesn't have init(Float) method
*/

Double 转换成 String:

1
2
3
4
5
6
7
8
9
// Double to String

// String
var doubl:Double = 10.25
"\(doubl)"
String.convertFromStringInterpolationSegment(doubl)

/* String(10.25) -> String doesn't have init(Double) method
*/
文章目录
  1. 1. String 转换成 Double:
  2. 2. String 转换成 Float:
  3. 3. String 转换成 Int:
  4. 4. Int 转换成 String:
  5. 5. Float 转换成 String:
  6. 6. Double 转换成 String: