当前位置:首页 > Windows程序 > 正文

C#中的字符编码问题

2021-03-28 Windows程序

该文件的编码为GB18030,每行的宽度为23个字符,其中第1-8列为员工姓名,第10-23列为工资额。现在我们要写一个C#程序求出该单位员工的平均工资,如下所示:

 1using System; 
 2using System.IO; 
 3using System.Text; 
 4 
 5namespace Skyiv.Ben.Test 
 6
 7  sealed class Avg 
 8  
 9    static void Main() 
10    
11      try 
12      
13        Encoding encode = Encoding.GetEncoding("GB18030"); 
14        using (StreamReader sr = new StreamReader("salary.txt", encode)) 
15        
16          decimal avg = 0
17          long rows = 0
18          for (; ; rows++
19          
20            string s = sr.ReadLine(); 
21            if (s == nullbreak
22            decimal salary = Convert.ToDecimal(s.Substring(914)); 
23            avg += salary; 
24          }
 
25          avg /= rows; 
26          Console.WriteLine(avg.ToString("N2")); 
27        }
 
28      }
 
29      catch (Exception ex) 
30      
31        Console.WriteLine("错误: " + ex.Message); 
32      }
 
33    }
 
34  }
 
35}
 
36

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/69042.html