【问题标题】:I keep getting error {"Input string was not in a correct format."}我不断收到错误{“输入字符串的格式不正确。”}
【发布时间】:2017-11-27 18:48:08
【问题描述】:

我在 Visual Studio 中进行了调试检查,amtBidded 和 ItemPrice 的值都是“xx.xx”,它们周围有引号,就好像它们是我想转换为小数或浮点数的字符串一样。 Parse 不断给我“输入字符串的格式不正确”。错误。我尝试了推荐的“文化”解决方案,但仍然出现错误。

我很茫然,任何帮助将不胜感激。

protected void ButtonClicktoBid_Click(object sender, EventArgs e)
    {
        String CurrentBidder = Context.User.Identity.GetUserName();
        String ItemId = ListBoxitemNum.SelectedItem.ToString();
        String ItemPrice = ListBoxCurrentPrice.SelectedItem.ToString();
        String amtBidded = TextBoxAmtBidded.Text.ToString();
        String SecondPlaceBidder = 
        ListBoxCurrentWinningBidder.SelectedItem.ToString();
        String dbMagic;

        if (float.Parse(amtBidded) < float.Parse(ItemPrice))
        {
            Response.Redirect("Default.aspx");
        }

【问题讨论】:

  • amtBidded 可能无法成功转换为float,尝试使用TryParse,它将返回布尔结果和转换后的输出
  • amtBidded的实际值是多少?
  • 感谢两位的回复。 Rainman 发送了一个非常适合我的解决方案。

标签: c# asp.net


【解决方案1】:

这样试试;

    var currentCulture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
    currentCulture.NumberFormat.CurrencyDecimalSeparator = ".";
    if (float.Parse(amtBidded, NumberStyles.Any, currentCulture) < float.Parse(ItemPrice, NumberStyles.Any, currentCulture))
    {
        Response.Redirect("Default.aspx");
    }

【讨论】:

  • 我非常感谢那个代码。它完美无缺。我一直对陌生人如何从他们的一天中抽出时间来提供帮助感到谦卑。我只希望有一天能变得足够好来回报这个人情。
猜你喜欢
  • 2014-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多