输入字符串的格式不正确(将字符串转换为十进制)

本文关键字:字符串 转换 十进制 格式 不正确 输入 | 更新日期: 2023-09-27 18:28:58

我正在尝试从*.aspx页面转换字符串:

JaveScript:

function updateOnClick() {
        if (!toIgnore) {
            refNo = document.getElementById("txtRef").value;
            note1000 = removeCommas(document.getElementById("txtNote_1000").value.substring(1));
            note100 = removeCommas(document.getElementById("txtNote_100").value.substring(1));
            note50 = removeCommas(document.getElementById("txtNote_50").value.substring(1));
            note20 = removeCommas(document.getElementById("txtNote_20").value.substring(1));
            note10 = removeCommas(document.getElementById("txtNote_10").value.substring(1));
            note5 = removeCommas(document.getElementById("txtNote_5").value.substring(1));
            note2 = removeCommas(document.getElementById("txtNote_2").value.substring(1));
            note1 = removeCommas(document.getElementById("txtNote_1").value.substring(1));
            coins = removeCommas(document.getElementById("txtCoins").value.substring(1));
            cheque = removeCommas(document.getElementById("txtCheque").value.substring(1));
            outstanding = removeCommas(document.getElementById("txtOutstanding").value.substring(1));
            total = removeCommas(document.getElementById("txtTotal").value.substring(1));
            collectable = removeCommas(document.getElementById("txtCollectable").value.substring(1));
            difference = removeCommas(document.getElementById("txtDifference").value.substring(1));
            collectionDate = document.getElementById(prefix + "txtDate").value;
            iniXmlHttp();
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    responseText = xmlhttp.responseText;
                    if (responseText == "") {
                        loadDailyCollectionTable();
                        document.getElementById("txtRef").focus();
                        document.getElementById("txtRef").select();
                    }
                }
            }
            xmlhttp.open("GET", "DailyCollectionPage.aspx?funcName=updateDailyCollection&RefNo=" + refNo +
            "&collectionDate=" + collectionDate + "&note1000=" + note1000 + "&note100=" + note100 +
            "&note50=" + note50 + "&note20=" + note20 + "&note10=" + note10 + "&note5=" + note5 +
            "&note2=" + note2 + "&note1=" + note1 + "&coins=" + coins + "&cheque=" + cheque +
            "&outstanding=" + outstanding + "&total=" + total + "&collectable=" + collectable + 
            "&difference=" + difference, true);
            xmlhttp.send();
        }
        else
            toIgnore = false;
    }

在Code Behind中,当我试图将字符串转换为十进制时,我在这一行中得到了错误:

dailyCollection.Notes_1000 = Convert.ToDecimal(Request["note1000"]);

错误为:INPUT STRING WAS NOT IN A CORRECT FORMAT.

有人能告诉我我的代码出了什么问题吗。任何帮助都将不胜感激!

输入字符串的格式不正确(将字符串转换为十进制)

INPUT STRING WAS NOT IN A CORRECT FORMAT.您确定您的代码总是返回value,而从不返回NULL吗?我认为当你的字符串是NullEmpty,所以你收到这个错误时,试着先检查null或空值。

  if(!string.IsNullOrEmpty(Convert.Tostring(Request["note1000"]))) 
{
   dailyCollection.Notes_1000 = Convert.ToDecimal(Request["note1000"].ToString());

你试过这样做吗?-

dailyCollection.Notes_1000 = Convert.ToDecimal(Request["note1000"].ToString());

此外,您应该使用Decimal.TryParse,它将处理此类错误情况。

例如:

decimal temp;
dailyCollection.Notes_1000  = Decimal.TryParse(Request["note1000"].ToString(),out temp)?temp:0.0M;