How does Textbox show price correctly?

三世轮回 提交于 2019-12-13 10:39:33

问题


I need to have a Textbox to show price; decimals like : 12,000,000 OR 1 000 (Price)
with MaskedTextBox i get : 120,000,00 OR 100 0

I would gladly appreciate it if anyone can help..
Thank You in advance


回答1:


Try adding this code to KeyUp event handler of your 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);
    }
}



回答2:


You can use DecimalFormat like this :

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);

Also, check this Link

Hope it will help you!




回答3:


 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


来源:https://stackoverflow.com/questions/16029177/how-does-textbox-show-price-correctly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!