UP 添加字符串转浮点
This commit is contained in:
@@ -47,6 +47,7 @@ typedef enum { // 定义枚举类型Type_t,包含不同数据类型
|
||||
float *:6,double *:10, \
|
||||
default: ((void)0))
|
||||
|
||||
|
||||
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) // 计算数组的元素个数
|
||||
#define in , // 定义逗号为in
|
||||
#define _foreach(e, a) for(size_t e = 0; e < ARRAY_SIZE(a); e++) // 实现foreach宏,遍历数组a,e为当前元素下标
|
||||
@@ -82,7 +83,7 @@ typedef enum { // 定义枚举类型Type_t,包含不同数据类型
|
||||
* @param I_Max: [输入] 数据最大值
|
||||
* @param O_Min: [输入] 需求最小值
|
||||
* @param O_Max: [输入] 需求最小值
|
||||
* @retval 需求处理值
|
||||
* @return 需求处理值
|
||||
* @example Mapping(128,0,4096,0.0,3.3) //0.10
|
||||
* ADC采集引脚电压
|
||||
**/
|
||||
@@ -111,6 +112,14 @@ float Mapping(float val, float I_Min, float I_Max, float O_Min, float O_Max);
|
||||
**/
|
||||
void BufPrint(char *name, void *buf, Type_t type, unsigned int len, unsigned char frame);
|
||||
|
||||
/**
|
||||
* @brief 字符串转浮点数
|
||||
* @param str: [输入] 输入的字符串
|
||||
* @return float 转换后的浮点数
|
||||
* @example Str2Float("3.14");
|
||||
**/
|
||||
float Str2Float(char *str);
|
||||
|
||||
/**
|
||||
* @brief 数组内容打印
|
||||
* @param arr: [输入] 标识名数组
|
||||
|
@@ -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));
|
||||
}
|
||||
|
Reference in New Issue
Block a user