網(wǎng)站首頁 編程語言 正文
float轉(zhuǎn)std::string 小數(shù)位數(shù)控制
std::stringstream 方式
?? ?float a = 1122.334455; ?? ?std::stringstream buf; ?? ?buf.precision(2);//覆蓋默認(rèn)精度 ?? ?buf.setf(std::ios::fixed);//保留小數(shù)位 ?? ?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的,兩位小數(shù) ?? ?std::string strCode(chCode); ?? ?delete []chCode;
string轉(zhuǎn)float顯示位數(shù)有誤;cout 的 precision 成員函數(shù)
問題描述
在進(jìn)行string轉(zhuǎn)float過程中,發(fā)現(xiàn)有些數(shù)顯示位數(shù)不同(存在數(shù)精度少了一位的情況,例如:0.1285354 轉(zhuǎn)換后,顯示 0.128535)
數(shù)據(jù)如下:
0.0281864
-0.0635702
0.0457153
0.1285354
-0.0254498
...
問題分析
后了解到 float 只顯示有效位數(shù) 6 位, 而 double 顯示有效位數(shù) 15 位
-
float
有效數(shù)字位為6 – 7位,字節(jié)數(shù)為4,指數(shù)長度為8位,小數(shù)長度為23位。取值范圍為 3.4E-38~3.4E+38。 -
double
有效數(shù)字位為15 – 16位,字節(jié)數(shù)為8,指數(shù)長度為11位,小數(shù)長度為52位。取值范圍為1.7E-308~1.7E+308。
隨即思考,是不是轉(zhuǎn)換后賦值到了float上,導(dǎo)致精度降低呢?
馬上修改賦值到double類型上,然而任然顯示有誤。
這才想到會不會使 cout 輸出精度的問題,搜索后發(fā)現(xiàn) cout 需要調(diào)用 precision() 成員函數(shù)來設(shè)置顯示精度,而 cout 默認(rèn)精度為6位有效數(shù)字,哈哈真是湊巧,跟 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 設(shè)置浮點(diǎn)數(shù)輸出精度方法
方法一(全局設(shè)置 cout 輸出精度)
#include <iostream> double tmp = 0.1234567890123456; cout.precision(16);?? ?//此處設(shè)置后,全局有效;cout浮點(diǎn)數(shù)輸出精度均為16 cout << tmp << endl;
方法二(全局設(shè)置 cout 輸出精度)
#include <iostream> #include <iomanip> double tmp = 0.1234567890123456; cout << setprecision(16) << tmp << endl; //此處設(shè)置后,全局有效;后面cout浮點(diǎn)數(shù)輸出精度均為16 cout << 0.1234567890123456 << endl;?? ?// 0.1234567890123456
原文鏈接:https://blog.csdn.net/Atticus_DingYu/article/details/107064540
相關(guān)推薦
- 2022-04-12 C++中標(biāo)準(zhǔn)線程庫的基本使用介紹_C 語言
- 2022-06-01 Python+Opencv實(shí)現(xiàn)計(jì)算閉合區(qū)域面積_python
- 2023-01-13 解決安裝torch后,torch.cuda.is_available()結(jié)果為false的問題_py
- 2022-09-15 .Net站點(diǎn)設(shè)置多個路由對應(yīng)同一個Action_實(shí)用技巧
- 2022-09-21 使用注解實(shí)現(xiàn)Redis緩存功能_Redis
- 2022-03-20 C++線程池實(shí)現(xiàn)代碼_C 語言
- 2022-05-13 C語言數(shù)據(jù)結(jié)構(gòu)之二叉樹詳解_C 語言
- 2022-01-08 關(guān)于git操作warning: adding embedded git repository: pp
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支