C#中关于double.ToString()的用法
作者:kingkee 时间:2021-12-06 13:12:22
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
标签:C#,double,ToString
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
spring mvc实现文件上传与下载功能
2022-05-13 09:50:14
Java中的break和continue关键字的使用方法总结
2022-07-13 11:50:46
Android CountDownTimer实现定时器和倒计时效果
2023-12-09 02:11:28
合并有序数组的实现(java与C语言)
2023-08-16 13:45:40
Unity中的PostProcessScene实用案例深入解析
2021-06-09 04:57:28
SpringMVC+MyBatis 事务管理(实例)
2022-07-30 11:04:40
![](https://img.aspxhome.com/file/2023/9/65579_0s.png)
JavaWeb详细讲述Cookie和Session的概念
2022-03-23 08:39:44
![](https://img.aspxhome.com/file/2023/2/61272_0s.png)
Qt QTableWidget基本操作及使用
2022-05-22 23:15:55
![](https://img.aspxhome.com/file/2023/5/117775_0s.gif)
C#实现猜数字小游戏
2023-01-13 16:39:38
![](https://img.aspxhome.com/file/2023/3/91373_0s.jpg)
c#操作xml帮助类分享(xml增删改查)
2022-03-02 04:09:21
vs 中C#项目读取JSON配置文件的方法
2022-09-22 15:04:05
Java基于Tcp/ip连接的多人交互聊天室
2022-03-31 17:47:35
![](https://img.aspxhome.com/file/2023/6/103846_0s.png)
轻松学习C#的方法
2023-06-21 13:22:32
![](https://img.aspxhome.com/file/2023/3/66573_0s.jpg)
Java中泛型的用法总结
2023-03-03 08:54:47
c#实现选择排序的示例
2021-10-15 00:32:27
![](https://img.aspxhome.com/file/2023/6/78116_0s.gif)
c#中oracle的to_date函数使用方法
2021-09-06 10:21:17
在Spring Boot2中使用CompletableFuture的方法教程
2022-09-27 14:18:51
C# 多线程中经常访问同一资源可能造成哪些问题
2022-09-08 05:22:47
C#使用LINQ查询操作符实例代码(一)
2021-11-27 01:41:57
![](https://img.aspxhome.com/file/2023/8/77258_0s.png)
Spring Boot 搭建 ELK正确看日志的配置流程
2022-08-28 17:13:46
![](https://img.aspxhome.com/file/2023/0/113260_0s.png)