繁体   English   中英

输入字符串格式不正确,从十进制到字符串?

[英]Input string was not in a correct format from decimal to string?

我正在尝试将小数转换为字符串,这是我的代码:我已更新代码,我有教科书查看货币值,不允许在运行时编辑它只是为了显示其他货币数字的结果。

      string sj;
    void calculaer()
    {
        try
        {
            if (String.IsNullOrEmpty(mtb_SALAIR02.Text)) return;
            mtb_SALAIR02.Text = string.Format("{0:#,###,##0.00}", decimal.Parse(mtb_SALAIR02.Text));
            sj = (decimal.Parse(mtb_SALAIR02.Text, CultureInfo.CurrentCulture) / 30).ToString();
            mtb_SJ02.Text = string.Format("{0:#,###,##0.00}", decimal.Parse(sj));

            if (String.IsNullOrEmpty(mtb_SJ02.Text)) return;
            mtb_SJ02.Text = string.Format("{0:#,###,##0.00}", decimal.Parse(mtb_SJ02.Text));
            sj = (decimal.Parse(mtb_SJ02.Text, CultureInfo.CurrentCulture) * decimal.Parse(mtb_NJ02.Text)).ToString();
            mtb_SM02.Text = string.Format("{0:#,###,##0.00}", decimal.Parse(sj));

            if (String.IsNullOrEmpty(mtb_SM02.Text)) return;
            mtb_SM02.Text = string.Format("{0:#,###,##0.00}", decimal.Parse(mtb_SM02.Text));
            sj = (decimal.Parse(mtb_SM02.Text, CultureInfo.CurrentCulture) - decimal.Parse(mtb_AVANCE02.Text) + decimal.Parse(mtb_RELQ02.Text)).ToString();
            mtb_NETPAIE02.Text = string.Format("{0:#,###,##0.00}", decimal.Parse(sj));

            if (String.IsNullOrEmpty(mtb_NETPAIE02.Text)) return;
            mtb_NETPAIE02.Text = string.Format("{0:#,##0,0#0.00}", decimal.Parse(mtb_NETPAIE02.Text));

            taxe_calc();


            sj = (string.IsNullOrEmpty(mtb_NETPAIE02.Text)? 0M: decimal.Parse(mtb_NETPAIE02.Text, CultureInfo.CurrentCulture)) + (string.IsNullOrEmpty(mtb_TAXE02.Text)? 0M: decimal.Parse(mtb_TAXE02.Text)).ToString();
            mtb_SAL_TAX02.Text = string.Format("{0:#,###,##0.00}", decimal.Parse(sj));
        }
        catch (Exception)
        {
            return;
        }
    }

我的代码的图像

在此输入图像描述

我通常会建议拆分代码,即

//decimal dcj = decimal.Parse(sj);
decimal dcj;
if (!decimal.TryParse(sj, out decimal))
{
    throw new ApplicationException("...");
}

mtb_SAL_TAX02.Text = string.Format("{0:#,###,##0.00}",dcj);

另外,如果你的字符串表示十进制可能有错,我宁愿选择tryparse。

试试吧。 您应该在解析时设置文化。 我认为您的计算机没有使用标准文化设置。

不要使用CurrentCulture,让我们更改为InvariantCulture或特定文化

var result = string.Format("{0:#,###,##0.00}", decimal.Parse("10,000.00", System.Globalization.CultureInfo.InvariantCulture));

或者你也可以根据需要设定特定的文化

var result = string.Format("{0:#,###,##0.00}", decimal.Parse("10,000.00", new System.Globalization.CultureInfo("en-EN")));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM