網站首頁 編程語言 正文
C# double.ToString()的用法
C# 中 double 類型的數據,有時需要格式化顯示為字符串(保留N位有效數字或者是保留N位小數),可以使用 double.ToString("參數") 方法。
下面列出幾個常用的方法
(F)Fixed point:string str1=temp.ToString("f1"); ? ? ? ? ? ? ?//保留一位小數 四舍五入 結果:3.1
(F)Fixed point:string str2=temp.ToString("f2"); ? ? ? ? ? ? ?//保留兩位小數,四舍五入 下面一次類推 結果:3.14
(N)Number:string str2=temp.ToString("N"); ? ? ? ? ? ? ? ? ? //保留 結果:3.14
(G)General (default):string str2=temp.ToString("G"); ? ?//保留 結果:3.1415926
(P)Percent:string str2=temp.ToString("P"); ? ? ? ? ? ? ? ? ? //保留 結果:314.16%
(E)Scientific:string str2=temp.ToString("E"); ? ? ? ? ? ? ? ? //保留 結果E:3.141593E+000
(C)Currency:string str2=temp.ToString("C"); ? ? ? ? ? ? ? ?//保留 結果:¥3.14
C# Double 按有效數字 ToString
將double轉換為n有效數字的字符
我找了各種帖子包括在微軟文檔搜索 ‘significant digits’;最接近的是ToString(“Gx”)
但是返回是不大于這個x有效位的字符串, 舉例
double a=1.2;
string s=a.ToString("G3");
得到1.2而不是1.20
所以就寫了一個函數
? ? ? ? string DoubleToStringSignificantDigits(double a, int SignificantDigits)
? ? ? ? {
? ? ? ? ? ? string formaterG = 'G' + SignificantDigits.ToString("N0");
? ? ? ? ? ? string strResult = a.ToString(formaterG);
? ? ? ? ? ? int resultLength = SignificantDigits;
? ? ? ? ? ? if (strResult.IndexOf('-') >= 0) resultLength++;
? ? ? ? ? ? if (strResult.IndexOf('.') >= 0) resultLength++;
? ? ? ? ? ? if (Math.Abs(a) < 1) resultLength++; //絕對值小于1,有一個整數0不算有效位
? ? ? ? ? ? if (strResult.Length < resultLength)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (strResult.IndexOf('.') < 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? strResult += '.';
? ? ? ? ? ? ? ? ? ? resultLength++;
?? ??? ?}
? ? ? ? ? ? ? ? strResult = strResult.PadRight(resultLength, '0');
? ? ? ? ? ? }
? ? ? ? ? ? return (strResult);
? ? ? ? ?}
結果
double[] x = new double[] { 100, 99, 12.12, 1.1234, 1.2, 0.2, 0.12345 , -0.2, -1.2, -123};
轉換
DoubleToStringSignificantDigits(x[i], 3)
得到
100
99.0
12.1
1.12
0.200
0.123
-0.200
-1.20
-123
總結
原文鏈接:https://blog.csdn.net/kingkee/article/details/100926729
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-11-03 關于golang?test緩存問題_Golang
- 2023-01-08 ubuntu1804搭建FTP服務器的詳細教程_FTP服務器
- 2023-03-22 Redis慢查詢日志及慢查詢分析詳解_Redis
- 2022-10-02 react函數組件useState異步,數據不能及時獲取到的問題_React
- 2022-04-04 react 報錯Assign arrow function to a variable before
- 2023-11-13 解決Selected processor does not support ARM mode `dm
- 2022-10-24 python正則表達中的re庫常用方法總結_python
- 2022-11-12 react項目中@路徑簡單配置指南_React
- 欄目分類
-
- 最近更新
-
- 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同步修改后的遠程分支