我如何将 strtok() 设为 return 十进制数而不是整数?
How would I get strtok() to return the decimal number and not whole numbers?
我有一个函数可以读取文件并将其作为网格打印出来。
这是文件(输入):
2 2
1.83 5.64
7.36 4.10
但是当它打印出矩阵时它只有 returns 个整数(输出):
2 2
1.00 5.00
7.00 4.00
如何获得打印十进制数的函数?
想要的输出(即):
2 2
1.83 5.64
7.36 4.10
你的问题不是 strtok
,而是这个:
double newVal = strtol(value, &ptr, 10);
strtol
将字符串转换为 long
,而不是 double
,因此它会丢弃小数部分。使用 strtod
代替:
double newVal = strtod( value, &ptr );
我有一个函数可以读取文件并将其作为网格打印出来。
这是文件(输入):
2 2
1.83 5.64
7.36 4.10
但是当它打印出矩阵时它只有 returns 个整数(输出):
2 2
1.00 5.00
7.00 4.00
如何获得打印十进制数的函数?
想要的输出(即):
2 2
1.83 5.64
7.36 4.10
你的问题不是 strtok
,而是这个:
double newVal = strtol(value, &ptr, 10);
strtol
将字符串转换为 long
,而不是 double
,因此它会丢弃小数部分。使用 strtod
代替:
double newVal = strtod( value, &ptr );