0

Suppose I have a decimal number 12345789.0 Also I have a RadioButtonList:

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

When I choose radio item I want to get result like this:

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

Result should be decimal too. Take a look that results after point should be different.

Can anyone give me advice how to do it.

serg
  • 35
  • 5

2 Answers2

1

You can exploit integer conversion a little bit to achieve what you want:

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

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

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

The resultString now holds the correct answer.

You would of course have to create a switch case or something to get the right number to divide with in the val / X - But this should help you enough to get what you want.

Chikilah
  • 490
  • 2
  • 8
  • Yes, I realize this is kind of a hacky method. I'd assume there's some ways to get around it with the Math library as well, but I haven't worked a whole lot with this library as of right now. You can find a reference here: http://msdn.microsoft.com/en-us/library/system.math(v=vs.110).aspx – Chikilah Dec 01 '13 at 12:18
  • I got you question all wrong I guess. The actual problem is more about rounding. Check out the answer in this question http://stackoverflow.com/questions/13522095/rounding-down-to-2-decimal-places-in-c-sharp – th1rdey3 Dec 01 '13 at 13:14
0

Are you looking for something like this -

aspx Page

<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>

Code behind

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();
}
th1rdey3
  • 4,176
  • 7
  • 30
  • 66