[英]How would I get strtok() to return the decimal number and not whole numbers?
I have a function that reads in a file and prints it out as a grid. 我有一个读取文件并将其打印为网格的函数。
This is the file (input): 这是文件(输入):
2 2
1.83 5.64
7.36 4.10
But when it prints out the matrix it only returns whole numbers (output): 但是,当打印出矩阵时,它仅返回整数(输出):
2 2
1.00 5.00
7.00 4.00
How do I get the function to print out the decimal number? 如何获得打印出十进制数字的函数?
Wanted Output (ie): 想要的输出(即):
2 2
1.83 5.64
7.36 4.10
Your problem isn't strtok
, it's this: 您的问题不是
strtok
,是这样的:
double newVal = strtol(value, &ptr, 10);
strtol
converts the string to a long
, not a double
, so it's discarding the fractional portion. strtol
将字符串转换为long
而不是double
,因此它将丢弃小数部分。 Use strtod
instead: 使用
strtod
代替:
double newVal = strtod( value, &ptr );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.