总结C#保留小数位数

开发 后端
本文介绍了C#保留小数位数的各种情况,以及C#保留小数位数及百分号的解决方法。看完本文你肯定有不少收获,希望本文能教会你更多东西。

本文向大家介绍C#保留小数位,可能好多人还不了解C#保留小数位,没有关系,看完本文你肯定有不少收获,希望本文能教会你更多东西。

1.简单的例子

System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();   
provider.NumberDecimalDigits =intDecLength; //要設定的小數位數   
double strCashAmt=Convert.ToDouble(this.txtCashAmt.Text); //先把控件內的值轉成double   
 
this.txtCashAmt.Text = strCashAmt.ToString("N",provider); //再利用ToString函數格式化小數位數  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

2.C#保留小数位N位,四舍五入 .

decimal d= decimal.Round(decimal.Parse("0.55555"),2);  
  • 1.

3.C#保留小数位N位四舍五入

Math.Round(0.55555,2)

Math.Round(0.55555,2)  
  • 1.

4,C#保留小数位N位四舍五入

double dbdata = 0.55555;   
string str1 = dbdata.ToString("f2");//fN 保留N位,四舍五入  
  • 1.
  • 2.

5.C#保留小数位N位四舍五入

string result = String.Format("{0:N2}", 0.55555);//2位   
 
string result = String.Format("{0:N3}", 0.55555);//3位  
  • 1.
  • 2.
  • 3.

6. C#保留小数位N位四舍五入

double s=0.55555;   
result=s.ToString("#0.00");//点后面几个0就保留几位  
  • 1.
  • 2.

C#保留小数位数,及百分号的解决方法:

1、用NumberFormatInfo类来解决:

System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();   
 
provider.PercentDecimalDigits = 2;//小数点保留几位数.   
provider.PercentPositivePattern = 2;//百分号出现在何处.   
double result = (double)1 / 3;//一定要用double类型.   
Response.Write(result.ToString("P", provider));  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

2、用toString方法.:

public string getRate(double hcount, double task)   
{   
string rValue;   
string temp = "";   
 
if (task == 0)   
{   
task = 1;   
}   
 
double db = (hcount / task) * 100;   
 
if (hcount >= task)   
{   
rValue = "100%";   
}   
else   
{   
rValue = db.ToString("#0.#0") + "%";   
}   
return rValue;   
}   
 
string str1 = String.Format("{0:N1}",56789); //result: 56,789.0   
string str2 = String.Format("{0:N2}",56789); //result: 56,789.00   
string str3 = String.Format("{0:N3}",56789); //result: 56,789.000   
string str8 = String.Format("{0:F1}",56789); //result: 56789.0   
string str9 = String.Format("{0:F2}",56789); //result: 56789.00   
string str11 =(56789 / 100.0).ToString("#.##"); //result: 567.89   
string str12 =(56789 / 100).ToString("#.##"); //result: 567   
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.

【编辑推荐】

  1. 解密C#-SQLite是如何移植的
  2. 看看如何透过JavaScript调用C#函数
  3. 浅析C#事件注册和注销
  4. 示例:C#通过AMO对象浏览SQL SERVER 2005 SSAS
  5. C#隐藏窗口的几种方法
责任编辑:book05 来源: hi.baidu
相关推荐

2009-08-27 13:27:50

C# this保留字

2009-08-26 11:03:58

C# GetOrdin

2009-08-13 18:21:52

C#学习笔记

2009-08-11 15:44:05

C#基本技巧

2009-08-13 18:02:50

C#基础概念

2009-08-17 17:04:29

C#转义字符

2009-08-27 16:54:59

C#开发技巧

2024-12-24 07:38:44

C#串口通信

2009-08-20 18:06:18

学习C#接口

2009-08-27 16:37:06

C#基础知识

2009-08-21 08:52:40

C#语言命名

2009-08-05 18:39:54

C#异常类

2009-08-13 18:13:27

C#学习经验

2009-08-13 12:50:45

C#基础知识

2009-09-09 13:31:15

C# TextBox

2009-08-11 14:20:41

C# .NET学习经验

2009-08-12 17:32:44

C#反射方法

2009-08-13 10:52:03

C#基础概念

2009-08-21 18:01:32

C#匿名方法

2009-08-20 17:17:02

C#哈希表
点赞
收藏

51CTO技术栈公众号