常用数字处理函数

1.对计算结果四舍五入(d:数,i小数位数)

 效果: 233.8763

 -->  233.88


 1   //d: 表示四舍五入的数字;  i: 保留的小数位数 
 2 
 3         public static double Round(double d, int i)
 4 
 5 {
 6 
 7 if (d >= 0)
 8 
 9 {
10 
11 += 5 * Math.Pow(10-(i + 1));
12 
13 }
14 
15 else
16 
17 {
18 
19 += -5 * Math.Pow(10-(i + 1));
20 
21 }
22 
23 string str = d.ToString();
24 
25 string[] strs = str.Split('.');
26 
27 int idot = str.IndexOf('.');
28 
29 string prestr = strs[0];
30 
31 string poststr = strs[1];
32 
33 if (poststr.Length > i)
34 
35 {
36 
37 poststr = str.Substring(idot + 1, i);//截取需要位数
38 
39             }
40 
41 if (poststr.Length <= 2)
42 
43 {
44 
45 poststr = poststr + "0";
46 
47 }
48 
49 string strd = prestr + "." + poststr;
50 
51 = Double.Parse(strd);//将字符串转换为双精度实数 
52 
53             return d;
54 
55 


2.将商品金额小写转换成大写

 效果: 1234566789

    -->壹拾贰亿叁仟肆佰伍拾陆万陆仟柒佰捌拾玖元


 1 private void Convert_Click(object sender, EventArgs e)
 2 
 3 {
 4 
 5 String[] Scale = { """""""""""""""""""""亿""""""""""""""" };
 6 
 7 String[] Base = { """""""""""""""""""" };
 8 
 9 String Temp = textBox1.Text.ToString();
10 
11 String Info = null;
12 
13 int index = Temp.IndexOf("."0, Temp.Length);//判断是否有小数点
14 
15             if (index != -1)
16 
17 {
18 
19 Temp = Temp.Remove(Temp.IndexOf("."), 1);
20 
21 for (int i = Temp.Length; i > 0; i--)
22 
23 {
24 
25 int Data = Convert.ToInt16(Temp[Temp.Length - i]);
26 
27 Info += Base[Data - 48];
28 
29 Info += Scale[i - 1];
30 
31 }
32 
33 }
34 
35 else
36 
37 {
38 
39 for (int i = Temp.Length; i > 0; i--)
40 
41 {
42 
43 
44 
45 int Data = Convert.ToInt16(Temp[Temp.Length - i]);
46 
47 Info += Base[Data - 48];
48 
49 Info += Scale[i + 1];
50 
51 }
52 
53 
54 
55 }
56 
57 
58 
59 textBox2.Text = Info;
60 
61 


3.设置货币值中使用的小数位数


 1        System.Globalization.NumberFormatInfo GN = new         System.Globalization.CultureInfo("zh-CN"false).NumberFormat;
 2 
 3 Int64 myInt = 12345;
 4 
 5 private void SetLL_Click(object sender, EventArgs e)
 6 
 7 {
 8 
 9 GN.CurrencyDecimalDigits = 4;
10 
11 MessageBox.Show(myInt.ToString("C", GN), "保留四位小数");
12 
13 

 


4.自定义货币值中的小数点


 1 System.Globalization.NumberFormatInfo GN = new System.Globalization.CultureInfo("zh-CN"false).NumberFormat;
 2 
 3 Int64 myInt = 123456789;
 4 
 5 private void button1_Click(object sender, EventArgs e)
 6 
 7 {
 8 
 9 GN.CurrencyDecimalSeparator = "$";
10 
11 MessageBox.Show("定义前:" + myInt.ToString("C"+ "\n" + "定义后:" + myInt.ToString("C", GN), "自定义小数点为$符");
12 
13 


5.自定义货币值中小数点左边每一组的位数 


 1 System.Globalization.NumberFormatInfo CN = new System.Globalization.CultureInfo("en-US"false).NumberFormat;
 2 
 3 Int64 myInt = 123456789012345;
 4 
 5 int[] mySizes1 = { 231 };
 6 
 7 int[] mySizes2 = { 232 };
 8 
 9 CN.CurrencyGroupSizes = mySizes1;
10 
11 MessageBox.Show("定义前:" + myInt.ToString("C"+ "\n" + "定义后:" + myInt.ToString("C", CN), "{ 2, 3, 1 }格式");
12 
13 CN.CurrencyGroupSizes = mySizes2;
14 
15 MessageBox.Show("定义前:" + myInt.ToString("C"+ "\n" + "定义后:" + myInt.ToString("C", CN), "{ 2, 3, 2 }格式"


6.自定义货币值中小数点左边数字分组字符


1 System.Globalization.NumberFormatInfo GN = new System.Globalization.CultureInfo("en-US"false).NumberFormat;
2 
3 Int64 myInt = 123456789;
4 
5 GN.CurrencyGroupSeparator = "";
6 
7 MessageBox.Show("定义前: " + myInt.ToString("C"+ "\n" + "定义后: " + myInt.ToString("C", GN), "分组字符用、号"


7.自定义百分比符号

效果: 默认符号: 10,257,865.84%
自定义符号&: 10,257,865.84&
自定义符号*: 10,257,865.84*
自定义符号#: 10,257,865.84#


 1  System.Globalization.NumberFormatInfo GN = new System.Globalization.CultureInfo("zh-CN"false).NumberFormat;
 2 
 3 double intPrecnt = 102578.6584;
 4 
 5 string strPrecnt = null;
 6 
 7 GN.PercentSymbol = "%";
 8 
 9 strPrecnt += "默认符号: " + intPrecnt.ToString("p", GN);
10 
11 GN.PercentSymbol = "&";
12 
13 strPrecnt += "\n自定义符号&: " + intPrecnt.ToString("p", GN);
14 
15 GN.PercentSymbol = "*";
16 
17 strPrecnt += "\n自定义符号*: " + intPrecnt.ToString("p", GN);
18 
19 GN.PercentSymbol = "#";
20 
21 strPrecnt += "\n自定义符号#: " + intPrecnt.ToString("p", GN);
22 
23 MessageBox.Show(strPrecnt, "设置效果"


8.自定义数字小数点右边的保留位数

效果: 原始数字: 4458524.2568412547

默认小数位数:4,458,524.26
保留三位小数:4,458,524.257
保留五位小数:4,458,524.25684
保留五位小数:4,458,524.2568413


 1 System.Globalization.NumberFormatInfo GN = new System.Globalization.CultureInfo("zh-CN"false).NumberFormat;
 2 
 3 double intNumber = 4458524.2568412547;
 4 
 5 string strNumber = "";
 6 
 7 strNumber += "默认小数位数:" + intNumber.ToString("N");
 8 
 9 GN.NumberDecimalDigits = 3;
10 
11 strNumber += "\n保留三位小数:" + intNumber.ToString("N", GN);
12 
13 GN.NumberDecimalDigits = 5;
14 
15 strNumber += "\n保留五位小数:" + intNumber.ToString("N", GN);
16 
17 GN.NumberDecimalDigits = 7;
18 
19 strNumber += "\n保留五位小数:" + intNumber.ToString("N", GN);
20 
21 MessageBox.Show(strNumber, "设置效果"


9.自定义数字小数点左边分组位数(从小数点开始向左)

 效果: 默认格式:711,413,414,517.12

{ 1, 3, 4 }格式:7114,1341,451,7.12
{ 2, 3, 0 }格式:7114134,145,17.12
{ 2, 6, 2 }格式:71,14,134145,17.12


 1 System.Globalization.NumberFormatInfo GN = new System.Globalization.CultureInfo("zh-CN"false).NumberFormat;
 2 
 3 double intNumber = 711413414517.12;
 4 
 5 string strNumber = null;
 6 
 7 strNumber += "默认格式:" + intNumber.ToString("N", GN);
 8 
 9 int[] MySizes1 = { 134 };
10 
11 GN.NumberGroupSizes = MySizes1;
12 
13 strNumber += "\n{ 1, 3, 4 }格式:" + intNumber.ToString("N", GN);
14 
15 int[] MySizes2 = { 230 };
16 
17 GN.NumberGroupSizes = MySizes2;
18 
19 strNumber += "\n{ 2, 3, 0 }格式:" + intNumber.ToString("N", GN);
20 
21 int[] MySizes3 = { 262 };
22 
23 GN.NumberGroupSizes = MySizes3;
24 
25 strNumber += "\n{ 2, 6, 2 }格式:" + intNumber.ToString("N", GN);
26 
27 MessageBox.Show(strNumber, "设置效果"


10.格式化输入数据为货币格式

效果: 输入: 12345

        输出: ¥12 345.00


 1             try
 2 
 3 {
 4 
 5 System.Globalization.NumberFormatInfo nfi = new System.Globalization.CultureInfo("zh-CN"false).NumberFormat;
 6 
 7 nfi.CurrencyGroupSeparator = " ";
 8 
 9 textBox2.Text = Convert.ToDouble(textBox1.Text).ToString("c", nfi);
10 
11 }
12 
13 catch (Exception ee)
14 
15 {
16 
17 MessageBox.Show(ee.Message);
18 
19 

posted @ 2008-09-25 16:54  TONYBINLJ  阅读(348)  评论(0编辑  收藏  举报