ASP.NET Decimal.Parse()

I have problem in webpage , Visual Studio claims that problem is with this line decimal newAmount = PLNamount * Decimal.Parse(item.Value); . The solution is crushed when I choose the Current (web page is simple current converter) .

this is listing of CurrencyConverter.aspx.cs

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class CurrencyConverter : System.Web.UI.Page
{
    protected void Page_Load(Object sender, EventArgs e)
    {
        if (this.IsPostBack == false)
        {
            // The HtmlSelect control accepts text or ListItem objects.
            Currency.Items.Add(new ListItem("Euros", "0.25"));
            Currency.Items.Add(new ListItem("US Dollar", "0.32"));
            Currency.Items.Add(new ListItem("British Pound", "0.205"));
        }
    }
    protected void Convert_ServerClick(object sender, EventArgs e)
    {

        decimal PLNamount;

        bool success = Decimal.TryParse(PLN.Value,out PLNamount);
        if (success)
        {
            PLNamount = Decimal.Parse(PLN.Value);
            ListItem item = Currency.Items[Currency.SelectedIndex];
            decimal newAmount = PLNamount * Decimal.Parse(item.Value); //prollematic line

            Result.InnerText = PLNamount.ToString() + " Polish PLN = ";
            Result.InnerText += newAmount.ToString() + " " + item.Text;
        }
        else Result.InnerText = "Invalid content";
    }
}

This is listing of CurrencyConverter.aspx

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="CurrencyConverter.aspx.cs" Inherits="CurrencyConverter" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Currency Converter</title>
</head>
<body>
Simple Currency Converter in ASP.NET web forms <br />

<form id="Form1" runat="server">
<div>
Convert: &nbsp;
<input type="text" ID="PLN" runat="server" />
&nbsp; Polish PLN to Euros.
<select ID="Currency" runat="server" />
<br /><br />
<input type="submit" value="OK" ID="Convert" runat="server"
OnServerClick="Convert_ServerClick" />
<br /><br />
<div style="font-weight: bold" ID="Result" runat="server"></div>
</div>
</form>

</body>
</html>

This Code is changed listing from apress "Pro ASP.NET 3.5 in C# 2008, Second Edition" (original version) from chapter 5. Link to source code apress.com/book/downloadfile/3803 . I have exactly the same problem in the same line

Original source code from the book

*aspx

Currency Converter

    <div style="border-right: thin ridge; padding-right: 20px; border-top: thin ridge;
        padding-left: 20px; padding-bottom: 20px; border-left: thin ridge; width: 531px;
        padding-top: 20px; border-bottom: thin ridge; font-family: Verdana; background-color: #FFFFE8">
    Convert: &nbsp;
    <input type="text" ID="US" runat="server" style="width: 102px" />&nbsp; U.S. dollars to &nbsp;
    <select ID="Currency" runat="server" />
    <br /><br />
    <input type="submit" value="OK" ID="Convert" runat="server" OnServerClick="Convert_ServerClick" />
    <input type="submit" value="Show Graph" ID="ShowGraph" runat="server" OnServerClick="ShowGraph_ServerClick" />
    <br /><br />
    <img ID="Graph" alt="Currency Graph" scr="" runat="server" />
    <br /><br />
    <div style="font-weight: bold" ID="Result" runat="server"></div>
  </div>
</form>

*aspx.cs

using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;

public partial class CurrencyConverter : System.Web.UI.Page { protected void Page_Load(Object sender, EventArgs e) { if (this.IsPostBack == false) { // The HtmlSelect control accepts text or ListItem objects. Currency.Items.Add(new ListItem("Euros", "0.85")); Currency.Items.Add(new ListItem("Japanese Yen", "110.33")); Currency.Items.Add(new ListItem("Canadian Dollars", "1.2")); } Graph.Visible = false; } protected void Convert_ServerClick(object sender, EventArgs e) { decimal amount; bool success = Decimal.TryParse(US.Value, out amount); if (success) { // Retrieve the selected ListItem object by its index number. ListItem item = Currency.Items[Currency.SelectedIndex];

        decimal newAmount = amount * Decimal.Parse(item.Value);
        Result.InnerText = amount.ToString() + " U.S. dollars = ";
        Result.InnerText += newAmount.ToString() + " " + item.Text;
    }
    else
    {
        Result.InnerText = "The number you typed in was not in the correct format. ";
        Result.InnerText += "Use only numbers.";
    }
}

protected void ShowGraph_ServerClick(object sender, EventArgs e)
{
    Graph.Src = "Pic" + Currency.SelectedIndex.ToString() + ".png";
    Graph.Visible = true;
}

}

Problem solved

This is link to print screen with this error. http://img210.imageshack.us/f/currencyerror.png/


I think the answer is localisation based. I suspect you are parsing "0.25" as a decimal in your a culture style which uses commas as decimal separators. What you need to do is to specify what style the numbers are in so that it interprets them correctly. One way to do this is:

decimal newAmount = PLNamount * Decimal.Parse(item.Value, CultureInfo.InvariantCulture.NumberFormat)

Hopefully people will comment if there is a nicer way of doing it.

Hope that helps. I've not confirmed that this is definitely your problem but if I try to parse the number as pl-PL (just guessing you are polish from the page) then it fails for me on that line you quoted so I reckon I'm on the right lines. :)

Edit to add: The other option is to change your dropdown to have values like "0,25" which should parse correctly. However, I personally would prefer to use the CultureInfo option.

Interestingly when I enter numbers into the box on the page it seems to recognise "0.9" and "0,9" as the same thing. Not sure why it works there but I guess its related to tryParse and Parse workign subtly differently.


You don't need to do both:

    bool success = Decimal.TryParse(PLN.Value,out PLNamount);
    if (success)
    {
        PLNamount = Decimal.Parse(PLN.Value);

If the TryParse succeeded then PLNamount will contain the correct value. Check the MSDN documentation for TryParse .

Also:

        ListItem item = Currency.Items[Currency.SelectedIndex];
        decimal newAmount = PLNamount * Decimal.Parse(item.Value); //prollematic line

if the value in item isn't a string representing a number Parse will throw an exception. For safety you should use TryParse on this value as well (if it's not already a numeric type of course).


you should delete the following line

 PLNamount = Decimal.Parse(PLN.Value);

if(success) ... then PLNamount are already parsed, it is an out parameter...

链接地址: http://www.djcxy.com/p/35902.html

上一篇: 如何为网站添加浏览器选项卡图标(favicon)?

下一篇: ASP.NET Decimal.Parse()