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);
}
文章来源: http://www.cnblogs.com/ziyiFly/archive/2008/09/16/1291937.html