c - 如何在C中对文本中的 float 进行四舍五入?

标签 c

所以我有这段文字

today average human lifespan in Western countries is approaching and exceeding 80 years.
1 Japan 82.6 123 19
2 Hong Kong 82.2 234 411
3 Iceland 81.8 345 26
4 Switzerland 81.7 456 7
5 Australia 81.2 567 500
...
80 Lithuania 73.0 800 2
...
194 Swaziland 39.6 142 212
195 133714 18.0 133 998
196 100110011 10.0 667 87351

我需要将本文中的 float 四舍五入为整数。我搜索了很多论坛,但似乎找不到我需要的东西。 该文本在 .txt 文件中给出。我的任务:“将实数(有小数)四舍五入为整数。更正后的文本应该位于新的 .txt 中”就是这样。

最佳答案

scanfsscanf 是提取字符串中任何内容的最佳 friend 。
floor 使用 full 来抑制浮点上的小数。然后可以用来对其进行舍入(使用它包括 math.h)...

格式字符串描述了预期的格式。函数返回找到的参数数量。

示例:

  • 初始化

    int id = -1;
    char country[160]; /* /!\ Warning country name length shall be fewer than 160 */
                       /*      Scanf don't care of this array parameter length. If it is too short */
                       /*      It will erase following memory...                 */
                       /* That is why scanf are often disparaging                   */
    float percent = 0.0;
    char a_number_as_string[10];
    int other_number = -1;
    char* Switzerland = "4 Switzerland 81.7654321 456 7";
    
  • 效果代码

     int ret = sscanf(Switzerland, "%d %s %f %s %d", &id, country, 
                      &percent, a_number_as_string, &other_number);
     if(ret == 5) {
         printf("~~ id: %d\n\tcountry: %s\n\tpercent: %.2f\n\tand    : "
                "(%s, %d)\n", id, country, percent, a_number_as_string,
                other_number);
    
         /////// ROUND
         printf("*** round");
         printf("\twith printf %%.1f = %.1f\n", percent);
         printf("\twith printf %%.2f = %.2f\n", percent);
         printf("\twith printf %%.3f = %.3f\n", percent);
         printf("\twith printf %%.4f = %.4f\n", percent);
    
         printf("*** With floor (included in math.h)\n");
         printf("\t1 decimal: %f\n", floor(percent*10)/10);
         printf("\t2 decimal: %f\n", floor(percent*100)/100);
         printf("\t3 decimal: %f\n", floor(percent*1000)/1000);
         printf("\t4 decimal: %f\n", floor(percent*10000)/10000);
     } else {
         printf("--> ret = %d", ret);
     }
    
  • 输出

    ~~ id: 4

    country: Switzerland
    percent: 81.70
    and : (456, 7)

    *** 圆形

    with printf %.1f = 81.8
    with printf %.2f = 81.77
    with printf %.3f = 81.765
    with printf %.4f = 81.7654

    *** 有底线(包含在 math.h 中)

    1 decimal: 81.700000
    2 decimal: 81.760000
    3 decimal: 81.765000
    4 decimal: 81.765400

此函数在 Unix、OSX、Unix 终端手册页中进行了描述:

  • man scanf
  • man sscanf
  • 男人楼层

或者您可以在网络上管理此管理器的多个副本中找到它,例如

关于c - 如何在C中对文本中的 float 进行四舍五入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40317323/

相关文章:

c - 找到两个序列的连接点

c - 使用 gcc 版本 4.3.3 构建 OpenSSL 1.1.1c 时出现问题

c - execve() 和共享文件描述符

c++ - 如何在启用显示隐藏文件和文件夹的情况下使用 C\C++ 隐藏

c - 这个赋值是什么意思,p=q 其中 p 和 q 都是结构指针?

c - = (~0);这是什么意思?

c - 使用 C 段错误进行字符串操作

c - 在 procfs.h 中找不到 pstatus_t (LINUX)

c - 为什么我们不能在某些进程上接受()套接字并从其子进程中接收()数据?

字符串指针数组的值可以被覆盖吗?