@@ -29,6 +29,15 @@ namespace StrUtils {
29
29
c == REMAINDER_SIGN;
30
30
}
31
31
32
+ /* *
33
+ * 将一个 ascii 数值转换成为字符串
34
+ * @param ascii 需要被转换的 ascii 数值
35
+ * @return 转换操作成功之后的字符串
36
+ */
37
+ std::string asciiToChar (int ascii) {
38
+ return {char (ascii)};
39
+ }
40
+
32
41
/* *
33
42
* 将一个数值字符传换成一个数值
34
43
*
@@ -37,11 +46,14 @@ namespace StrUtils {
37
46
*/
38
47
int charToInteger (char c) {
39
48
if (StrUtils::IsANumber (c)) {
40
- return c - 0x30 ;
49
+ return c - 48 ;
41
50
} else {
42
- std::string data = &" 您在进行字符与数值之间的转换时,由于字符的不正确导致无法成功转换,错误字符:" [c];
43
- throw ME::MExceptional (data.append (
44
- &" \n When you are converting characters to numeric values, the conversion cannot be successful due to incorrect characters. Error characters:" [c]));
51
+ std::string temp_ex_data = " 您在进行字符与数值之间的转换时,由于字符的不正确导致无法成功转换" ;
52
+ std::string temp_res = asciiToChar (c);
53
+ temp_ex_data.append (
54
+ " \n When you are converting characters to numeric values, the conversion cannot be successful due to incorrect characters." )
55
+ .append (temp_res);
56
+ throw ME::WrongFormat (temp_ex_data);
45
57
}
46
58
}
47
59
@@ -58,6 +70,16 @@ namespace StrUtils {
58
70
int floatSize = 0 ;
59
71
bool isInt = true ;
60
72
unsigned long long length = s.length ();
73
+ if (length <= 0 ) {
74
+ throw ME::WrongFormat (
75
+ " 您在调用 stringToDouble 函数的时候 不能传递空字符串!\n You cannot pass an empty string when calling the stringToDouble function!" );
76
+ }
77
+ // 判断是否为负数
78
+ bool isF = s[0 ] == MINUS_SIGN;
79
+ if (isF) {
80
+ s = s.substr (1 );
81
+ length -= 1 ;
82
+ }
61
83
for (int i = 0 ; i < length; i++) {
62
84
char c = s[i];
63
85
if (c != DECIMAL_POINT && c != EMPTY) {
@@ -87,7 +109,7 @@ namespace StrUtils {
87
109
// 计算出来数值本身
88
110
const double res = intRes + floatRes / (double ) NumberUtils::PowerOfTen (10 , floatSize);
89
111
// 判断是否为负数,如果不是负数直接返回值
90
- return s[ 0 ] == MINUS_SIGN ? -res : res;
112
+ return isF ? -res : res;
91
113
}
92
114
93
115
std::vector<std::string> splitByChar (const std::string &data, char splitChar) {
0 commit comments