Click here to Skip to main content
15,900,724 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I have two textboxes one to display quantity and the other for price.What i want is to remove decimals from the quantity and format the second value "price" upto two decimals.

The problem is that the textboxes gives the decimal value however the code gives the correct value.Below is my code for review.

Example: My code gives a value 1 for 1.00 but displayes 1.00 as the value in the textboxes.

Any Help would be appreciated.
ASPX
Below is the aspx
ASP.NET
<asp:Panel ID="pnlOrderCurrent" 
               style="Z-INDEX: 101; LEFT: 23px; POSITION: absolute; TOP: 103px" 
               runat="server" Height="102px" Width="327px">
          <table style="height: 98px; width: 324px">
              
                  <tr>
                      <td>
                          <asp:Image ID="Image1" runat="server" Height="30px" 
                              ImageUrl="~/Images/customer.png" />
                      </td>
                      <td>
                          <asp:TextBox ID="txtCurrentCustomerCode" Runat="server" Height="12px" 
                              Width="70px" AutoPostBack="True" 
                              ontextchanged="txtCurrentCustomerCode_TextChanged">
                          </td>
                      <td>
                          <asp:Image ID="Image2" runat="server" Height="30px" 
                              ImageUrl="~/Images/showvalqty.png" />
                      </td>
                      <td>
 <asp:TextBox ID="txtCurrentValue" Runat="server" Height="12px" Width="70px" Style="text-align: right"> 
                          </td>
  </tr>
 <tr>
                      <td>
 <asp:Image ID="Image3" runat="server" Height="30px" 
                              ImageUrl="~/Images/neworder.png" />
                     </td>
                      <td>
   <asp:TextBox ID="txtCurrentOrder" Runat="server" Height="12px" Width="70px" 
                              >
                      </td>
                      <td>
   <asp:Image ID="Image4" runat="server" Height="30px" 
                              ImageUrl="~/Images/showorder.png" />
                      </td>
                      <td>
 <asp:TextBox ID="txtCurrentQty" Runat="server" Height="12px" Width="70px" Style="text-align: right">
                     </td>
                     
                    </tr>
                    
                   </table>   







public void orderitemsummary()
    {
        DataTable dtfrqtyval = new DataTable();
        connfordata.Open();
        SqlCommand cmdfrupdcust = new SqlCommand("sp_SRXO_OrderItemSummary", connfordata);
        SqlDataAdapter dafrqtyval = new SqlDataAdapter(cmdfrupdcust);
        cmdfrupdcust.Parameters.AddWithValue("@W113_XMLID", txtCurrentOrder.Text.Trim());
        cmdfrupdcust.CommandType = CommandType.StoredProcedure;
        cmdfrupdcust.ExecuteNonQuery();
        dafrqtyval.Fill(dtfrqtyval);
        connfordata.Close();

        double qtyval = Convert.ToDouble(dtfrqtyval.Rows[0]["SumQty"].ToString());
        string currentval = dtfrqtyval.Rows[0]["SumVal"].ToString();
        int qtyvalcast = (int)Math.Round(qtyval);

        string[] array = currentval.Split('.');
        string first = array[0];
        string second = array[1];

        string sub = second.Substring(0, 2);

        string final = (first + "." + sub).Trim();


        txtCurrentValue.Text = final;
        txtCurrentQty.Text = qtyvalcast.ToString();
    }
Posted
Updated 6-Oct-13 21:32pm
v4
Comments
Azee 7-Oct-13 3:23am    
Could you all post aspx side of the code (TextBoxes) in your question?
Amit Karyekar 7-Oct-13 3:30am    
ya please see the edited question.
Azee 7-Oct-13 3:41am    
I am a little confused, do you want you TextBoxes to have values with Decimal or without?

See I have changed your code with comment
See the edited code are following below

C#
double qtyval = Convert.ToDouble(dtfrqtyval.Rows[0]["SumQty"].ToString());
     decimal currentval = Convert.ToDecimal(dtfrqtyval.Rows[0]["SumVal"]);
     //string currentval = dtfrqtyval.Rows[0]["SumVal"].ToString();
    // int qtyvalcast = (int)Math.Round(qtyval);

     //string[] array = currentval.Split('.');
     //string first = array[0];
     //string second = array[1];

     //string sub = second.Substring(0, 2);

     //string final = (first + "." + sub).Trim();


     txtCurrentValue.Text = Math.Round(currentval, 2).ToString();// final;
     txtCurrentQty.Text = qtyval.ToString("n"); //qtyvalcast.ToString();
 
Share this answer
 
v2
Comments
Amit Karyekar 7-Oct-13 3:48am    
doesnt work out still decimals for txtCurrentValue.Text = 16.0000
and for
txtCurrentQty.Text = qtyval.ToString("n") = 1.00
Try

Convert.ToInt32(); // instead of Convert.ToDouble()

and for 2 decimal value try

Math.Round(mydoublevalue, 2);



Hope will help you out.
 
Share this answer
 
Comments
Amit Karyekar 7-Oct-13 4:16am    
even this approach doesnt work out.The value is perfect in the debug mode but when i see it from on the screen automatically decimals are added.
See it's simple do it this way
txtCurrentQty.Text = ((int?)qtyval).ToString();
 
Share this answer
 
hello professional

try this
may help you

txtCurrentQty.Text = Convert.ToInt32(qtyvalcast);

Happy to Help!!!
 
Share this answer
 
plz write below code in our code -

string second = array[1];
string sub = second .Substring(0,second .LastIndexOf(".")+3);
C#
string final = (first + "." + sub).Trim();


        txtCurrentValue.Text = final;
        txtCurrentQty.Text = qtyvalcast.ToString();
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900