目录
一、用double.TryParse()和NumberFormat属性设置
using System.Globalization;
namespace _050
{
public partial class Form1 : Form
{
private GroupBox? groupBox1;
private Button? button1;
private ComboBox? comboBox1;
private TextBox? textBox2;
private TextBox? textBox1;
private Label? label3;
private Label? label2;
private Label? label1;
public Form1()
{
InitializeComponent();
Load += Form1_Load;
}
private void Form1_Load(object? sender, EventArgs e)
{
//
// button1
//
button1 = new Button
{
Location = new Point(138, 51),
Name = "button1",
Size = new Size(75, 23),
TabIndex = 6,
Text = "转化",
UseVisualStyleBackColor = true
};
button1.Click += Button1_Click;
//
// comboBox1
//
comboBox1 = new ComboBox
{
FormattingEnabled = true,
Location = new Point(93, 49),
Name = "comboBox1",
Size = new Size(39, 25),
TabIndex = 5,
};
comboBox1.Items.AddRange([
"1",
"2",
"3",
]);
//
// textBox2
//
textBox2 = new TextBox
{
Location = new Point(93, 80),
Name = "textBox2",
Size = new Size(120, 23),
TabIndex = 4
};
//
// textBox1
//
textBox1 = new TextBox
{
Location = new Point(93, 22),
Name = "textBox1",
Size = new Size(120, 23),
TabIndex = 3
};
//
// label3
//
label3 = new Label
{
AutoSize = true,
Location = new Point(11, 86),
Name = "label3",
Size = new Size(68, 17),
TabIndex = 2,
Text = "转化结果:"
};
//
// label2
//
label2 = new Label
{
AutoSize = true,
Location = new Point(11, 57),
Name = "label2",
Size = new Size(68, 17),
TabIndex = 1,
Text = "小数点位:"
};
//
// label1
//
label1 = new Label
{
AutoSize = true,
Location = new Point(11, 28),
Name = "label1",
Size = new Size(68, 17),
TabIndex = 0,
Text = "输入金额:"
};
//
// groupBox1
//
groupBox1 = new GroupBox
{
Location = new Point(12, 12),
Name = "groupBox1",
Size = new Size(234, 117),
TabIndex = 0,
TabStop = false,
Text = "设置小数点后位数"
};
groupBox1.Controls.Add(button1);
groupBox1.Controls.Add(comboBox1);
groupBox1.Controls.Add(textBox2);
groupBox1.Controls.Add(textBox1);
groupBox1.Controls.Add(label3);
groupBox1.Controls.Add(label2);
groupBox1.Controls.Add(label1);
groupBox1.SuspendLayout();
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 17F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(258, 141);
Controls.Add(groupBox1);
Name = "Form1";
StartPosition = FormStartPosition.CenterScreen;
Text = "设置货币小数点后位数";
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
}
private void Button1_Click(object? sender, EventArgs e)
{
if (double.TryParse(textBox1!.Text, out double temp_value))//判断输入是否正确
{
NumberFormatInfo GN = //实例化NumberFormatInfo对象
new CultureInfo("zh-CN", false).NumberFormat;
GN.CurrencyDecimalDigits = Convert.ToInt32(comboBox1!.Text);//设置保留位数
textBox2!.Text = temp_value.ToString("C", GN);//将输入的小数转换为货币形式
}
else
{
MessageBox.Show("请输入正确的数值!", "提示!");//用户提示信息
}
}
}
}
?
? ? ? ? ?请详见本文作者写的其他文章,C#用double.TryParse(String, Double)方法将字符串类型数字转换为数值类型-CSDN博客 ?https://wenchm.blog.csdn.net/article/details/135593983
????????获取或设置 NumberFormatInfo,它定义适合区域性的、显示数字、货币和百分比的格式。?
public virtual System.Globalization.NumberFormatInfo NumberFormat { get; set; }
属性值
NumberFormatInfo
一个 NumberFormatInfo,它定义适合区域性的、显示数字、货币和百分比的格式。
例外
ArgumentNullException
该属性设置为 null。
InvalidOperationException
设置了 NumberFormat 属性或任意 NumberFormatInfo 属性,但 CultureInfo 为只读。
// CultureInfo.NumberFormat 属性
using System.Globalization;
namespace ConsoleApp12
{
public class SamplesCultureInfo
{
public static void Main()
{
// Creates and initializes a CultureInfo.
CultureInfo myCI = new("en-US", false);
// Clones myCI and modifies the DTFI and NFI instances associated with the clone.
CultureInfo myCIclone = (CultureInfo)myCI.Clone();
myCIclone.DateTimeFormat.AMDesignator = "a.m.";
myCIclone.DateTimeFormat.DateSeparator = "-";
myCIclone.NumberFormat.CurrencySymbol = "USD";
myCIclone.NumberFormat.NumberDecimalDigits = 4;
// Displays the properties of the DTFI and NFI instances associated with the original and with the clone.
Console.WriteLine("DTFI/NFI PROPERTY\tORIGINAL\tMODIFIED CLONE");
Console.WriteLine("DTFI.AMDesignator\t{0}\t\t{1}", myCI.DateTimeFormat.AMDesignator, myCIclone.DateTimeFormat.AMDesignator);
Console.WriteLine("DTFI.DateSeparator\t{0}\t\t{1}", myCI.DateTimeFormat.DateSeparator, myCIclone.DateTimeFormat.DateSeparator);
Console.WriteLine("NFI.CurrencySymbol\t{0}\t\t{1}", myCI.NumberFormat.CurrencySymbol, myCIclone.NumberFormat.CurrencySymbol);
Console.WriteLine("NFI.NumberDecimalDigits\t{0}\t\t{1}", myCI.NumberFormat.NumberDecimalDigits, myCIclone.NumberFormat.NumberDecimalDigits);
}
}
}
/*
运行结果:
DTFI/NFI PROPERTY ORIGINAL MODIFIED CLONE
DTFI.AMDesignator AM a.m.
DTFI.DateSeparator / -
NFI.CurrencySymbol $ USD
NFI.NumberDecimalDigits 3 4
*/