網站首頁 編程語言 正文
一、int轉string
#include <iostream> #include <string> int main() { double f = 23.43; double f2 = 1e-9; double f3 = 1e40; double f4 = 1e-40; double f5 = 123456789; std::string f_str = std::to_string(f); std::string f_str2 = std::to_string(f2); // 注意:返回 "0.000000" std::string f_str3 = std::to_string(f3); // 注意:不返回 "1e+40". std::string f_str4 = std::to_string(f4); // 注意:返回 "0.000000" std::string f_str5 = std::to_string(f5); std::cout << "std::cout: " << f << '\n' << "to_string: " << f_str << "\n\n" << "std::cout: " << f2 << '\n' << "to_string: " << f_str2 << "\n\n" << "std::cout: " << f3 << '\n' << "to_string: " << f_str3 << "\n\n" << "std::cout: " << f4 << '\n' << "to_string: " << f_str4 << "\n\n" << "std::cout: " << f5 << '\n' << "to_string: " << f_str5 << '\n'; }
輸出
std::cout: 23.43
to_string: 23.430000
?
std::cout: 1e-09
to_string: 0.000000
?
std::cout: 1e+40
to_string: 10000000000000000303786028427003666890752.000000
?
std::cout: 1e-40
to_string: 0.000000
?
std::cout: 1.23457e+08
to_string: 123456789.000000
二、string轉int
#include <iostream> #include <string> int main() { std::string str1 = "45"; std::string str2 = "3.14159"; std::string str3 = "31337 with words"; std::string str4 = "words and 2"; int myint1 = std::stoi(str1); int myint2 = std::stoi(str2); int myint3 = std::stoi(str3); // 錯誤: 'std::invalid_argument' // int myint4 = std::stoi(str4); std::cout << "std::stoi(\"" << str1 << "\") is " << myint1 << '\n'; std::cout << "std::stoi(\"" << str2 << "\") is " << myint2 << '\n'; std::cout << "std::stoi(\"" << str3 << "\") is " << myint3 << '\n'; //std::cout << "std::stoi(\"" << str4 << "\") is " << myint4 << '\n'; }
結果:
std::stoi("45") is 45
std::stoi("3.14159") is 3
std::stoi("31337 with words") is 31337
原文鏈接:https://blog.csdn.net/m0_37316917/article/details/82712017
相關推薦
- 2023-05-22 shell腳本自動輸入用戶名和密碼的實現_linux shell
- 2022-04-19 idea如何解決jar包沖突
- 2023-01-04 Opencv實現鼠標事件與窗口互動功能過程_python
- 2022-05-06 C#迭代器方法介紹_C#教程
- 2022-03-19 使用Docker部署Spring?Boot項目的實現步驟_docker
- 2022-10-07 C語言直接插入排序算法介紹及示例_C 語言
- 2023-02-02 python保留兩位小數的3種方法實例_python
- 2022-05-27 hive中的幾種join到底有什么區別_數據庫其它
- 最近更新
-
- 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同步修改后的遠程分支