说明: 平时编程中总会遇到数字处理问题, 这里将自己平时总结的一些数字处理小技巧结合MSDN上相关的介绍, 列举一些常用的数字处理技术.
原理非常简单, 不再细说, 只图自己和大家引用或参考时方便.
1.对计算结果四舍五入(d:数,i小数位数)
效果: 233.8763
--> 233.88

计算结果四舍五入CODE
//d: 表示四舍五入的数字; i: 保留的小数位数
public static double Round(double d, int i)
{
if (d >= 0)
{
d += 5 * Math.Pow(10, -(i + 1));
}
else
{
d += -5 * Math.Pow(10, -(i + 1));
}
string str = d.ToString();
string[] strs = str.Split('.');
int idot = str.IndexOf('.');
string prestr = strs[0];
string poststr = strs[1];
if (poststr.Length > i)
{
poststr = str.Substring(idot + 1, i);//截取需要位数
}
if (poststr.Length <= 2)
{
poststr = poststr + "0";
}
string strd = prestr + "." + poststr;
d = Double.Parse(strd);//将字符串转换为双精度实数
return d;
}
2.将商品金额小写转换成大写
效果: 1234566789
-->壹拾贰亿叁仟肆佰伍拾陆万陆仟柒佰捌拾玖元

将金额小写转化为大写CODE
private void Convert_Click(object sender, EventArgs e)
{
String[] Scale = { "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾", "佰", "仟" };
String[] Base = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
String Temp = textBox1.Text.ToString();
String Info = null;
int index = Temp.IndexOf(".", 0, Temp.Length);//判断是否有小数点
if (index != -1)
{
Temp = Temp.Remove(Temp.IndexOf("."), 1);
for (int i = Temp.Length; i > 0; i--)
{
int Data = Convert.ToInt16(Temp[Temp.Length - i]);
Info += Base[Data - 48];
Info += Scale[i - 1];
}
}
else
{
for (int i = Temp.Length; i > 0; i--)
{
int Data = Convert.ToInt16(Temp[Temp.Length - i]);
Info += Base[Data - 48];
Info += Scale[i + 1];
}
}
textBox2.Text = Info;
}
3.设置货币值中使用的小数位数

设置货币值中使用的小数Code
System.Globalization.NumberFormatInfo GN = new System.Globalization.CultureInfo("zh-CN", false).NumberFormat;
Int64 myInt = 12345;
private void SetLL_Click(object sender, EventArgs e)
{
GN.CurrencyDecimalDigits = 4;
MessageBox.Show(myInt.ToString("C", GN), "保留四位小数");
}
4.自定义货币值中的小数点

Code
System.Globalization.NumberFormatInfo GN = new System.Globalization.CultureInfo("zh-CN", false).NumberFormat;
Int64 myInt = 123456789;
private void button1_Click(object sender, EventArgs e)
{
GN.CurrencyDecimalSeparator = "$";
MessageBox.Show("定义前:" + myInt.ToString("C") + "\n" + "定义后:" + myInt.ToString("C", GN), "自定义小数点为$符");
}
5.自定义货币值中小数点左边每一组的位数

Code
System.Globalization.NumberFormatInfo CN = new System.Globalization.CultureInfo("en-US", false).NumberFormat;
Int64 myInt = 123456789012345;
int[] mySizes1 = { 2, 3, 1 };
int[] mySizes2 = { 2, 3, 2 };
CN.CurrencyGroupSizes = mySizes1;
MessageBox.Show("定义前:" + myInt.ToString("C") + "\n" + "定义后:" + myInt.ToString("C", CN), "{ 2, 3, 1 }格式");
CN.CurrencyGroupSizes = mySizes2;
MessageBox.Show("定义前:" + myInt.ToString("C") + "\n" + "定义后:" + myInt.ToString("C", CN), "{ 2, 3, 2 }格式");
6.自定义货币值中小数点左边数字分组字符

Code
System.Globalization.NumberFormatInfo GN = new System.Globalization.CultureInfo("en-US", false).NumberFormat;
Int64 myInt = 123456789;
GN.CurrencyGroupSeparator = "、";
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#

Code
System.Globalization.NumberFormatInfo GN = new System.Globalization.CultureInfo("zh-CN", false).NumberFormat;
double intPrecnt = 102578.6584;
string strPrecnt = null;
GN.PercentSymbol = "%";
strPrecnt += "默认符号: " + intPrecnt.ToString("p", GN);
GN.PercentSymbol = "&";
strPrecnt += "\n自定义符号&: " + intPrecnt.ToString("p", GN);
GN.PercentSymbol = "*";
strPrecnt += "\n自定义符号*: " + intPrecnt.ToString("p", GN);
GN.PercentSymbol = "#";
strPrecnt += "\n自定义符号#: " + intPrecnt.ToString("p", GN);
MessageBox.Show(strPrecnt, "设置效果", MessageBoxButtons.OK, MessageBoxIcon.Information);
8.自定义数字小数点右边的保留位数
效果: 原始数字: 4458524.2568412547
默认小数位数:4,458,524.26
保留三位小数:4,458,524.257
保留五位小数:4,458,524.25684
保留五位小数:4,458,524.2568413

Code
System.Globalization.NumberFormatInfo GN = new System.Globalization.CultureInfo("zh-CN", false).NumberFormat;
double intNumber = 4458524.2568412547;
string strNumber = "";
strNumber += "默认小数位数:" + intNumber.ToString("N");
GN.NumberDecimalDigits = 3;
strNumber += "\n保留三位小数:" + intNumber.ToString("N", GN);
GN.NumberDecimalDigits = 5;
strNumber += "\n保留五位小数:" + intNumber.ToString("N", GN);
GN.NumberDecimalDigits = 7;
strNumber += "\n保留五位小数:" + intNumber.ToString("N", GN);
MessageBox.Show(strNumber, "设置效果", MessageBoxButtons.OK, MessageBoxIcon.Information);
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

Code
System.Globalization.NumberFormatInfo GN = new System.Globalization.CultureInfo("zh-CN", false).NumberFormat;
double intNumber = 711413414517.12;
string strNumber = null;
strNumber += "默认格式:" + intNumber.ToString("N", GN);
int[] MySizes1 = { 1, 3, 4 };
GN.NumberGroupSizes = MySizes1;
strNumber += "\n{ 1, 3, 4 }格式:" + intNumber.ToString("N", GN);
int[] MySizes2 = { 2, 3, 0 };
GN.NumberGroupSizes = MySizes2;
strNumber += "\n{ 2, 3, 0 }格式:" + intNumber.ToString("N", GN);
int[] MySizes3 = { 2, 6, 2 };
GN.NumberGroupSizes = MySizes3;
strNumber += "\n{ 2, 6, 2 }格式:" + intNumber.ToString("N", GN);
MessageBox.Show(strNumber, "设置效果", MessageBoxButtons.OK, MessageBoxIcon.Information);
10.格式化输入数据为货币格式
效果: 输入: 12345
输出: ¥12 345.00

Code
try
{
System.Globalization.NumberFormatInfo nfi = new System.Globalization.CultureInfo("zh-CN", false).NumberFormat;
nfi.CurrencyGroupSeparator = " ";
textBox2.Text = Convert.ToDouble(textBox1.Text).ToString("c", nfi);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端