Textbox如何正确显示价格?

时间:2013-04-16 05:06:07

标签: c# textbox

我需要Textbox来显示价格;小数点如:12,000,000 OR 1 000 (Price)
MaskedTextBox我得到:120,000,00或100 0

如果有人能提供帮助,我会很高兴 提前谢谢

3 个答案:

答案 0 :(得分:1)

您可以像这样使用DecimalFormat

Double number = Double.valueOf(text);

DecimalFormat dec = new DecimalFormat("#.00 EUR");
String credits = dec.format(number);

TextView tt = (TextView) findViewById(R.id.creditsView);
tt.setText(credits);

另外,请检查此Link

希望它会对你有所帮助!

答案 1 :(得分:1)

 decimal a = 12000000;
        Console.WriteLine(a.ToString());
        //output=> 12000000

        Console.WriteLine(a.ToString("N"));
        //output=>12,000,000.00

        Console.WriteLine(a.ToString("N1"));
        //output=>12,000,000.0

        Console.WriteLine(a.ToString("N0"));
        //output=>12,000,000

答案 2 :(得分:1)

尝试将此代码添加到KeyUp

TextBox事件处理程序中
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
    if (!string.IsNullOrEmpty(textBox1.Text))
    {
        System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
        int valueBefore = Int32.Parse(textBox1.Text, System.Globalization.NumberStyles.AllowThousands);
        textBox1.Text = String.Format(culture, "{0:N0}", valueBefore);
        textBox1.Select(textBox1.Text.Length, 0);
    }
}