基于CultureInfo.CurrentCulture的计算 [英] Calculations Based on CultureInfo.CurrentCulture

查看:69
本文介绍了基于CultureInfo.CurrentCulture的计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有两个文本框,一个用于数量,另一个用于输入单价。



我想做的是:



int或decimal TotalPrice =单价*数量。



我在文本框中的操作是:



Hi,

I have two text boxes, One will be used quantity and the other will be used to enter a Unit price.

what I would like to do is :

int or decimal TotalPrice = Unit price * quantity.

what I do in the textbox is:

private void Price_Text_Leave(object sender, EventArgs e)
{
    Price_Text.Text = Price_Text.Text.Replace('.', ',');
    Double value;
    if (Double.TryParse(Price_Text.Text, out value))
        Price_Text.Text = String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0:C2}", value);
    else
        Price_Text.Text = String.Empty;
}


private void Price_Text_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.' && e.KeyChar != ',')
    {
        e.Handled = true;
    }
}







我试过的是:



decimal TotalPriceValue = Convert.ToDecimal(UnitPrice.ToString())* Convert.ToDecimal(Quantity_Text.Text.ToString());







int TotalPriceValue = int.parse(UnitPrice.ToString())* int.parse(Quantity_Text.Text.ToString());



我得到的错误是:

输入字符串格式不正确。



错误是因为货币信(我的情况是R)和点。



我这样做是为了克服错误:




What I have tried is:

decimal TotalPriceValue = Convert.ToDecimal(UnitPrice.ToString()) * Convert.ToDecimal(Quantity_Text.Text.ToString());

and

int TotalPriceValue = int.parse(UnitPrice.ToString()) * int.parse(Quantity_Text.Text.ToString());

the error I get is :
Input string was not in a correct format.

The error is because of the currency letter(Im my case "R") amd the point "."

I have done this to overcome the error:

string UnitPrice = Price_Text.Text.Replace(',', '.');
UnitPrice = UnitPrice.Replace('R', '0');
UnitPrice = UnitPrice.Replace(' ', '0');









但是我计算结束后我在编号中的正确位置插入小数点的问题。





我还有其他的计算可以帮助吗?



请原谅我的问题我是c#的新手。





but then I have the issue of inserting the decimal point at the correct place in the number after my calculation is done.


Is there any other calculation I can rather do to help this?

Please excuse my question I am new to c#.

推荐答案

为什么要将所有内容作为字符串进行管理?在进行计算时,字符串不是很有用。

你要做的是从你的输入中得到一些小数,并按照这些小数进行计算。

类似于:

Why are you managing everything as string? A string is not very useful when it comes to make computations.
What you have to do is to get some decimal from your inputs, and make the computations on these decimals.
Something like:
decimal price;
int quantity;
decimal totalPriceValue;
if (decimal.TryParse(Price_Text.Text, out price) && int.TryParse(Quantity_Text.Text, out quantity)) {
   totalPriceValue = price * quantity;
}
else {
   // Here there was a problem parsing the price and/or the quantity. You have to decide what to do in this case
}





希望这会有所帮助。



PS:在已经是字符串的属性(即TextBoxes的Text属性)上调用ToString()没有意义。



Hope this helps.

PS: there is no point in calling ToString() on a property which already is a string (i.e., the Text property of your TextBoxes).


这篇关于基于CultureInfo.CurrentCulture的计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆