網站首頁 編程語言 正文
float轉std::string 小數位數控制
std::stringstream 方式
?? ?float a = 1122.334455; ?? ?std::stringstream buf; ?? ?buf.precision(2);//覆蓋默認精度 ?? ?buf.setf(std::ios::fixed);//保留小數位 ?? ?buf << a << "文字"; ?? ?std::string str; ?? ?str = buf.str();
sprintf 方式
? ? float a = 1122.334455; ?? ?char* chCode; ?? ?chCode = new(std::nothrow)char[20]; ?? ?sprintf(chCode, "%.2lf", a);// .2 是控制輸出精度bai的,兩位小數 ?? ?std::string strCode(chCode); ?? ?delete []chCode;
string轉float顯示位數有誤;cout 的 precision 成員函數
問題描述
在進行string轉float過程中,發現有些數顯示位數不同(存在數精度少了一位的情況,例如:0.1285354 轉換后,顯示 0.128535)
數據如下:
0.0281864
-0.0635702
0.0457153
0.1285354
-0.0254498
...
問題分析
后了解到 float 只顯示有效位數 6 位, 而 double 顯示有效位數 15 位
-
float
有效數字位為6 – 7位,字節數為4,指數長度為8位,小數長度為23位。取值范圍為 3.4E-38~3.4E+38。 -
double
有效數字位為15 – 16位,字節數為8,指數長度為11位,小數長度為52位。取值范圍為1.7E-308~1.7E+308。
隨即思考,是不是轉換后賦值到了float上,導致精度降低呢?
馬上修改賦值到double類型上,然而任然顯示有誤。
這才想到會不會使 cout 輸出精度的問題,搜索后發現 cout 需要調用 precision() 成員函數來設置顯示精度,而 cout 默認精度為6位有效數字,哈哈真是湊巧,跟 float 精度一樣。
修改后代碼如下:
#include <iostream> #include <string> #include <string.h> #include <stdlib.h> using namespace std; int main(int argc, char *argv[]) { ?? ?const string tmp_str = "0.1285354"; ?? ?float tmp_f = 0; ?? ?double tmp = 0; ?? ?cout.precision(16); ?? ?cout << sizeof(tmp_f) << "--" << sizeof(tmp) << endl; ?? ?cout << stof(tmp_str) << endl; ?? ?cout << stod(tmp_str) << endl; ?? ?cout << stold(tmp_str) << endl; ?? ?cout << strtod(tmp_str.c_str(), NULL) << endl; ?? ?cout << atof(tmp_str.c_str()) << endl; ?? ?tmp = 0.1234567890123456; ?? ?cout << tmp << endl; ?? ?return 0; }
程序輸出
nvidia@nx:~/pengjing/cuda$ ./location?
4--8
0.1285354048013687
0.1285354
0.1285354
0.1285354
0.1285354
0.1234567890123456
cout 設置浮點數輸出精度方法
方法一(全局設置 cout 輸出精度)
#include <iostream> double tmp = 0.1234567890123456; cout.precision(16);?? ?//此處設置后,全局有效;cout浮點數輸出精度均為16 cout << tmp << endl;
方法二(全局設置 cout 輸出精度)
#include <iostream> #include <iomanip> double tmp = 0.1234567890123456; cout << setprecision(16) << tmp << endl; //此處設置后,全局有效;后面cout浮點數輸出精度均為16 cout << 0.1234567890123456 << endl;?? ?// 0.1234567890123456
原文鏈接:https://blog.csdn.net/Atticus_DingYu/article/details/107064540
相關推薦
- 2022-10-25 IDEA創建spring spring項目并且注冊到nacos的SpringCloud示例
- 2022-11-19 詳解C語言內核中的鏈表與結構體_C 語言
- 2022-09-15 C#獲取文件名和文件路徑的兩種實現方式_C#教程
- 2021-12-12 C語言的常量和字符串_C 語言
- 2022-09-06 Redis與本地緩存的結合實現_Redis
- 2022-03-15 When allowCredentials is true, allowedOrigins cann
- 2022-11-14 .NET?Core?Web?APi類庫內嵌運行的方法_實用技巧
- 2022-08-10 詳細講解Swift中的類型占位符_Swift
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支