How come the discount amount textbox and total price aren't calculating right?

Hello Everyone,

I am working with ASP.NET web form 4.6 and I have created a web form, but I need help with the following:

How come when I enter fractions into the sales price textbox and integers into the discount percentage textbox, and then press the ‘Calculate’ button, it incorrectly calculates the discount amount and total price? What I want is for the calculate button to correctly calculate the fractions (like 120.45, etc ) and for discount amount to accept integers but for any integer that gets put into the textbox, it eventually converts it to a double (for example 20 represents 0.20). What do I need to do in order for different controls to function correctly. Here are my Default.aspx and Default.aspx.cs file code.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="XEx02Quotation.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Price quotation</title>
    <style type="text/css">
        .auto-style1 {
            width: 54%;
            height: 213px;
        }
        .auto-style15 {
            width: 114px;
            height: 23px;
        }
        .auto-style16 {
            width: 114px;
        }
        .auto-style17 {
            width: 114px;
            height: 28px;
        }
        .auto-style18 {
            width: 193px;
            height: 23px;
        }
        .auto-style20 {
            width: 193px;
            height: 28px;
        }
        .auto-style21 {
            width: 193px;
        }
        .auto-style22 {
            margin-left: 12px;
        }
        .auto-style23 {
            margin-left: 16px;
        }
        .auto-style25 {
            width: 193px;
            height: 5px;
        }
        .auto-style26 {
            width: 114px;
            height: 5px;
        }
        .auto-style27 {
            width: 143px;
            height: 23px;
        }
        .auto-style28 {
            width: 143px;
            height: 5px;
        }
        .auto-style29 {
            width: 143px;
        }
        .auto-style30 {
            width: 143px;
            height: 28px;
        }
        .auto-style31 {
            width: 143px;
            height: 25px;
        }
        .auto-style32 {
            width: 193px;
            height: 25px;
        }
        .auto-style33 {
            width: 114px;
            height: 25px;
        }
    </style>
</head>
<body>

    <form id="form1" runat="server">
        
        <h1>Price quotation</h1>
        
        <br />
        <br />
        <table class="auto-style1">
            <tr>
                <td class="auto-style27">Sales Price</td>
                <td class="auto-style18">
                    <asp:TextBox ID="TextBox1" runat="server" CssClass="auto-style23" Width="173px" Font-Bold="True"></asp:TextBox>
                </td>
                <td class="auto-style15"></td>
            </tr>
            <tr>
                <td class="auto-style27"></td>
                <td class="auto-style18"></td>
                <td class="auto-style15"></td>
            </tr>
            <tr>
                <td class="auto-style27">Discount percent</td>
                <td class="auto-style18">
                    <asp:TextBox ID="TextBox2" runat="server" CssClass="auto-style22" Width="169px"></asp:TextBox>
                </td>
                <td class="auto-style15"></td>
            </tr>
            <tr>
                <td class="auto-style28">Discount amount</td>
                <td class="auto-style25">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<asp:Label ID="Label1" runat="server" Font-Bold="True" Text="Label"></asp:Label>
                </td>
                <td class="auto-style26"></td>
            </tr>
            <tr>
                <td class="auto-style27"></td>
                <td class="auto-style18"></td>
                <td class="auto-style15"></td>
            </tr>
            <tr>
                <td class="auto-style30">Total price</td>
                <td class="auto-style20">
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <asp:Label ID="Label2" runat="server" Font-Bold="True" Text="Label"></asp:Label>
                </td>
                <td class="auto-style17"></td>
            </tr>
            <tr>
                <td class="auto-style31">
                    </td>
                <td class="auto-style32"></td>
                <td class="auto-style33"></td>
            </tr>
            <tr>
                <td class="auto-style29">
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Calculate" Width="90px" />
                </td>
                <td class="auto-style21">&nbsp;</td>
                <td class="auto-style16">&nbsp;</td>
            </tr>
        </table>
        
    </form>

</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace XEx02Quotation
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
       
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                int SalesPrice = Convert.ToInt32(TextBox1.Text);
                // When a user inputs an integer, I want it to be able to handle input of fractions
                // without getting an exception error. 



                // If a user inputs a whole number or fractional number (like 20.25), it should be able
                // to handle it.
                double DiscountPercentage = Convert.ToDouble(TextBox2.Text);
                

                double discountValue = this.CalculateDiscountValue(SalesPrice, DiscountPercentage);
                double TotalPrice = this.TotalPriceCalculate(SalesPrice, discountValue);
                Label1.Text = discountValue.ToString("c");
                Label2.Text = TotalPrice.ToString("c"); 
            
            }
        }
        protected double CalculateDiscountValue(int SalesPrice, double DiscountPercentage)
        {
           double discountAmount = SalesPrice * DiscountPercentage;
            return discountAmount; 
        }

        protected double TotalPriceCalculate(int SalesPrice, double discountAmount)
        {
            double TotalPrice = SalesPrice - discountAmount;
            return TotalPrice; 
        }


    }
        
}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.