鱼C论坛

 找回密码
 立即注册
查看: 2212|回复: 1

浮点型转字符串时精度丢失

[复制链接]
发表于 2017-7-7 10:26:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 new_guard 于 2017-7-7 11:23 编辑

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5. #include "atlstr.h"
  6. #include <cstdio>

  7. using namespace std;

  8. void main()
  9. {
  10.         float mag[6] = {-0.14189115,0.039332587,-0.044832617,0.83130932,0.0039432552,0.028655458};
  11.         ofstream out;
  12.         string temp_t;
  13.         char buffer[15];

  14.         out.open("first.txt");

  15.         if(!out.is_open())
  16.         {
  17.                 cerr << "打开out文件出错!\n";
  18.                 return;
  19.         }


  20.         for(int i = 0; i < 6; i++)
  21.         {
  22.                 sprintf_s(buffer, "%0.*f", 10, mag[i]);                        //此处代码重要
  23.                 temp_t = buffer;       
  24.                 cout << temp_t << "\t";                                //见下图显示结果
  25.                 out << temp_t <<" ";       
  26.                 temp_t = "";
  27.         }

  28.         out.close();

  29.         return;
  30. }
复制代码


原本想保留指定位数的小数,不足位数补零,但遇到奇葩问题是乱补数,我就凌乱了......................
1.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-7-7 10:42:09 | 显示全部楼层
给大家分享一个不错的网站:http://asoftmurmur.com/
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 20:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表