如何从值中删除小数。 [英] how to remove decimals from a value.

查看:112
本文介绍了如何从值中删除小数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本框,一个用于显示数量,另一个用于显示价格。我想要的是从数量中删除小数,并将第二个值价格格式化为两位小数。



问题是文本框给出了十进制值,但代码给出了正确的值。下面是我的审查代码。



示例:我的代码为1.00提供值1,但在文本框中显示1.00作为值。



任何帮助将不胜感激。

ASPX

以下是aspx

 <   asp:Panel     ID   =  pnlOrderCurrent    

style = Z-INDEX:101;左:23px;位置:绝对; TOP:103px

runat = < span class =code-keyword> server 高度 = 102px 宽度 = 327px < span class =code-keyword>>
< table style = height:98px;宽度:324像素 >

< tr >
< < span class =code-leadattribute> td >
< asp:图像 ID = Image1 runat = server 高度 = 30px

ImageUrl = < span class =code-keyword>〜/ Images / customer.png / >
< / td >
< td >
< asp :TextBox ID = txtCurrentCustomerCode Runat = server 高度 = 12px

宽度 = 70px AutoPostBack = True

< span class =code-attribute> ontextchanged = txtCurrentCustomerCode_TextChanged >
< / td >
< td >
< asp:Image ID = Image2 < span class =code-attribute> runat = server 高度 = 30px < span class =code-attribute>

< span class =code-attribute> < span class =code-attribute> ImageUrl = 〜/ Images / showvalqty.png / >
< / td >
< td >
< asp:TextBox ID = txtCurrentValue Runat = server 高度 = 12px Width = 70px 样式 = text-align:右 >
< / td >
< / tr >
< tr >
< td >
< asp:图像 ID = Image3 runat = server 高度 = 30px

ImageUrl = 〜 /Images/neworder.png\" / >
< / td >
< td >
< asp:TextBox ID = txtCurrentOrder Runat = server 高度 = 12px 宽度 = 70px

< span class =code-attribute>
>
< / td >
< td >
< asp:Image ID = Image4 runat = server 高度 = 30px

< span class =code-attribute> ImageUrl = 〜/ Images / showorder.png / > ;
< / td >
< td >
< asp:TextBox ID = txtCurrentQty Runat = server 高度 = 12px 宽度 = 70px 样式 = 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();
}

解决方案

看到我已经用注释更改了你的代码

查看编辑后的代码是以下是



  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();

尝试



Convert.ToInt32(); //而不是Convert.ToDouble()



和2位小数值试试



Math.Round (mydoublevalue,2);







希望能帮到你。


看到它很简单就这样做

 txtCurrentQty.Text =((int?)qtyval).ToString(); 


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: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();
    }

解决方案

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

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();


Try

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

and for 2 decimal value try

Math.Round(mydoublevalue, 2);



Hope will help you out.


See it's simple do it this way

txtCurrentQty.Text = ((int?)qtyval).ToString();


这篇关于如何从值中删除小数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆