简体   繁体   English

如何获取strtok()返回十进制数字而不是整数?

[英]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.

相关问题 我怎样才能摆脱零,如果输入小数,它会打印小数,但如果输入整数,它会打印.00? - How can I get rid of zeros, if decimal is entered it prints decimal, but if whole number is entered it prints .00? 我将如何使用 strtok 逐字比较 - How would i Use strtok to compare word by word 我对我的输出感到困惑,该输出给出了用户输入的数字的总和的最接近平均值(将小数点四舍五入为整数) - I am confused about my output that gives the closest average of sum of numbers entered by the user.(rounds the decimal to a whole number) 我如何确保用户在C中输入整数而不是小数? - how do i make sure the user put a whole number and not a decimal in c? 如何获取十进制数 position - How to get number at decimal position 如何逐位获取任意十进制数 - how can I get bit-by-bit of an arbitrary decimal number 我如何使用 fgets 和 strtok 来获取多个输入? - How do i use fgets and strtok to get multiple inputs? 如何获得与出现次数相对应的字母? - How would I get the letter corresponding to the number of occurences to sort? 如何使用strtok求和? - How to add up numbers using strtok? 如何修改我的代码,以便找到仅包含奇数的素数数量 - How can I modify my code, so it would find number of prime numbers that contain only odd digits
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM