如何在C#中删除逗号以进行计算 [英] How to remove commas for calculation in C#

查看:116
本文介绍了如何在C#中删除逗号以进行计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页表单,其中包含使用字符串代码格式化的文本框。



I have a web form that has a textbox that is formatted using a string code.

TextBoxTHUG.Text = string.Format("{0:0,0}", double.Parse(TextBoxTHUG.Text));





此代码将1234567变为1,234,567。我还有一个文本框文本更改代码来为此文本框进行计算。





This code turns 1234567 into 1,234,567. I also have a textbox text change code to do calculations for this textbox.

protected void TextBoxTHUG_TextChanged(object sender, EventArgs e)
    {
        int i = Convert.ToInt32(TextBoxTHUG.Text);
        int j = 12;
        TextBoxTHUGDR.Text = Convert.ToString(i / j);
        TextBoxTHG.Focus();
    }





我试图让它来计算put得到这个错误输入字符串的格式不正确 。有没有办法保持格式和计算或我必须删除格式然后计算?



I am trying to get it to calculate put get this error "Input string was not in a correct format". Is there a way to keep the format and calculate or do I have to remove the format and then calculate?

推荐答案

你不能计算字符串。您需要将其转换为数字。

尝试 Int32.TryParse [ ^ ]方法。





示例:

You can't calculate strings. You need to convert it to numbers.
Try Int32.TryParse[^] method.


Example:
String sNumber = "123,456,789";
int iDenominator = 12;
NumberStyles numStyle = NumberStyles.AllowThousands ;
CultureInfo culture = new CultureInfo("en-US");
int retValue = 0;

bool resultOfConversion = Int32.TryParse(sNumber, numStyle, culture, out retValue);

if (resultOfConversion == true)
    retValue = (int)retValue / iDenominator;


Console.WriteLine("Result: {0}", retValue );
Console.ReadKey();





注意:效果很好;)



Note: works perfect ;)


最简单的解决方案是使用< a href =http://msdn.microsoft.com/en-us/library/system.string.replace(v=vs.110).aspx> String.Replace Method [ ^ ]。
Easiest solution is to use String.Replace Method[^].


您好,



检查值是否为空白。



if(TextBoxTHUG.Text!=)

{

....

}



问候,

Jatinath Jadhav
Hi,

Check for value is not blank.

if(TextBoxTHUG.Text!="")
{
....
}

Regards,
Jatinath Jadhav


这篇关于如何在C#中删除逗号以进行计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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