在 Eval() 中计算
Calculation in Eval()
我正在做一个网站来显示产品和价格,但我在计算时遇到了一些错误。
<asp:Button ID="SellPrice1"
Visible="true"
BackColor="#9933FF"
Width="200px"
BorderStyle="solid"
BorderColor="#ffffff"
BorderWidth="0px"
ForeColor="white"
Font-Underline="false"
runat="server"
CommandArgument='<%# Eval("Barcode")%>'
CommandName="Add"
Text='<%# "Normal price: RM" + String.Format("{0:0.00}", Convert.ToInt32(Eval("SellPrice_1")) * 1.06, "{0:0.00}") %>'
Font-Size="12px"
Font-Bold="true"></asp:Button>
<asp:Button ID="SellPrice2"
Visible="true"
BackColor="#9933FF"
Width="200px"
BorderStyle="solid"
BorderColor="#ffffff"
BorderWidth="0px"
ForeColor="white"
Font-Underline="false"
runat="server"
CommandArgument='<%# Eval("Barcode")%>'
CommandName="Add"
Text='<%# "Member price: RM" + Eval("SellPrice_2", "{0:0.00}") %>'
Font-Size="12px"
Font-Bold="true"></asp:Button>
Price of the first item: HEINZ SALAD CREAM ORIGINAL 285G (8.0189)
以上是我调用2个价格显示的源代码(正常价格,会员价格)
两个数据当前具有相同的数量。
你们看到了,我的正常含税价格是1.06。
代码工作正常,但是当我使用计算器计算实际金额时,所有计算都是错误的,除了第二个是 275.00,如我所附的图片所示。
所以我的问题是,我怎样才能确保所有的计算都是正确的?如果我的信息不够清楚,请告诉我。
I found out that my problem is actually the calculation round off all the decimal places only it calculate the amount. but how can i fix this?
抱歉给您带来麻烦,我找到了解决方案,我会 post 以供将来参考。
正如 here 所讨论的那样,他将数字强制为十进制,例如,我对数字 1.06 也这样做了,最终结果是这样
Text='<%# "Normal price: RM" + String.Format("{0:0.00}", Convert.ToDecimal(Eval("SellPrice_1")) * 1.06m) %>'
如您所见,我首先 Convert.ToDecimal
为了计算包括小数位。
然后我得到错误
Operator '*' cannot be applied to operands of type 'decimal' and 'double'
这是我强制输入数字的地方
1.06 become 1.06m
最后我得到了我的最终结果。
再次抱歉给您带来麻烦,感谢ИвоНедев抽出时间
我正在做一个网站来显示产品和价格,但我在计算时遇到了一些错误。
<asp:Button ID="SellPrice1"
Visible="true"
BackColor="#9933FF"
Width="200px"
BorderStyle="solid"
BorderColor="#ffffff"
BorderWidth="0px"
ForeColor="white"
Font-Underline="false"
runat="server"
CommandArgument='<%# Eval("Barcode")%>'
CommandName="Add"
Text='<%# "Normal price: RM" + String.Format("{0:0.00}", Convert.ToInt32(Eval("SellPrice_1")) * 1.06, "{0:0.00}") %>'
Font-Size="12px"
Font-Bold="true"></asp:Button>
<asp:Button ID="SellPrice2"
Visible="true"
BackColor="#9933FF"
Width="200px"
BorderStyle="solid"
BorderColor="#ffffff"
BorderWidth="0px"
ForeColor="white"
Font-Underline="false"
runat="server"
CommandArgument='<%# Eval("Barcode")%>'
CommandName="Add"
Text='<%# "Member price: RM" + Eval("SellPrice_2", "{0:0.00}") %>'
Font-Size="12px"
Font-Bold="true"></asp:Button>
Price of the first item: HEINZ SALAD CREAM ORIGINAL 285G (8.0189)
以上是我调用2个价格显示的源代码(正常价格,会员价格) 两个数据当前具有相同的数量。
你们看到了,我的正常含税价格是1.06。 代码工作正常,但是当我使用计算器计算实际金额时,所有计算都是错误的,除了第二个是 275.00,如我所附的图片所示。
所以我的问题是,我怎样才能确保所有的计算都是正确的?如果我的信息不够清楚,请告诉我。
I found out that my problem is actually the calculation round off all the decimal places only it calculate the amount. but how can i fix this?
抱歉给您带来麻烦,我找到了解决方案,我会 post 以供将来参考。
正如 here 所讨论的那样,他将数字强制为十进制,例如,我对数字 1.06 也这样做了,最终结果是这样
Text='<%# "Normal price: RM" + String.Format("{0:0.00}", Convert.ToDecimal(Eval("SellPrice_1")) * 1.06m) %>'
如您所见,我首先 Convert.ToDecimal
为了计算包括小数位。
然后我得到错误
Operator '*' cannot be applied to operands of type 'decimal' and 'double'
这是我强制输入数字的地方
1.06 become 1.06m
最后我得到了我的最终结果。
再次抱歉给您带来麻烦,感谢ИвоНедев抽出时间