UP 添加字符串转浮点

This commit is contained in:
JiXieShi
2024-06-02 12:57:33 +08:00
parent 1f0ba41616
commit e6d089e400
5 changed files with 62 additions and 8 deletions

View File

@@ -42,4 +42,32 @@ void BufPrint(char *name, void *buf, Type_t type, unsigned int len, unsigned cha
}
}
printf("\n");
}
}
float Str2Float(char *str) {
int integer = 0;
double decimal = 0;
int sign = 1;
int decimalFlag = 0;
int decimalWeight = 1;
if (*str == '-') {
sign = -1;
str++;
}
while (*str != '\0') {
if (*str == '.') {
decimalFlag = 1;
str++;
continue;
}
if (!decimalFlag) {
integer = integer * 10 + (*str - '0');
} else {
decimal = decimal * 10 + (*str - '0');
decimalWeight *= 10;
}
str++;
}
decimal /= decimalWeight;
return float(sign * (integer + decimal));
}