根据用户选择返回或计算十进制数

时间:2013-12-01 11:04:59

标签: c# asp.net

假设我有一个十进制数12345789.0 我还有一个RadioButtonList:

<asp:RadioButtonList ID="RadioButtonList1" runat="server">  
    <asp:ListItem>1</asp:ListItem>  
    <asp:ListItem>1000</asp:ListItem>  
    <asp:ListItem>1000000</asp:ListItem>  
</asp:RadioButtonList>

当我选择广播项目时,我想得到这样的结果:

Case 1: 123456789.0/1 = 123456789.0
Case 1000: 123456789.0/1000 = 123456.7
Case 1000000: 123456789.0/1000000 = 123.45

结果也应为小数。 看看结果应该是不同的结果。

任何人都可以给我建议如何做。

2 个答案:

答案 0 :(得分:1)

您可以稍微利用整数转换来实现您想要的效果:

decimal val = 123456789;
decimal result = val / 1000000;

result = result * 100;
int converter = (int)result;
result = converter / 100m;

string resultString = result.ToString("0.##");

resultString现在保持正确的答案。

你当然必须创建一个开关案例或其他东西,以便在val / X中得到正确的数字 - 但这应该足以帮助你获得你想要的东西。

答案 1 :(得分:0)

你在找这样的东西 -

aspx页面

<asp:Label id="lbl" runat="server"></asp:Label>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">  
    <asp:ListItem>1</asp:ListItem>  
    <asp:ListItem>1000</asp:ListItem>  
    <asp:ListItem>1000000</asp:ListItem>  
</asp:RadioButtonList>

代码

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
    decimal val = 123456789.0m;
    switch(RadioButtonList1.SelectedValue)
    {
        case "1":
        case "1000":
            lbl.Text = (val/Convert.ToDecimal(RadioButtonList1.SelectedValue)).ToString("0.#");
            break;
        case "1000000":
            lbl.Text = (val/Convert.ToDecimal(RadioButtonList1.SelectedValue)).ToString("0.##");
            break;
        default:
            break;
    }
    lbl.Text = (val/Convert.ToDecimal(RadioButtonList1.SelectedValue)).ToString();
}