1

I am fairly new to all of this and am really struggling to find any examples of what I am trying to do.

all I want to create is a simple for where you enter some details and it performs a calculation on it.

I can get as far as adding and multiplying etc.. but when it comes to getting a decimal answer out I can get it to work.

and as an extra i want to use a drop down as a variable say do one calculation based on the drop down say * 1.4 if its male and * 1.01 if its female

below is what I have so far

@{
    var total = 0;
    var totalMessage = "";
    if(IsPost) {

        var age_= Request["frmage"];
        var weight_ = Request["frmweight"];
        var SerCre_ = Request["frmSerCre"];
        var sexfactor_ = Request["frmGender"];

        var age = age_.AsInt();       
        var weight= weight_.AsDecimal();
        var SerCre = SerCre_.AsDecimal();



        total =   (((140-age)*weight)*sexfactor)/SerCre ;
        totalMessage = "Total = " + total;
    }
}


 <form method="post">
    <p><label for="text1">Age:</label>
      <input type="text" name="frmAge" size="3"/>Years
    </p>
    <p><label for="text2">Weight:</label>
      <input type="text" name="frmWeight" />in Kg (1st = 6.35kg)
    </p>
    <p><label for="text3">Serum Creatinine:</label>
      <input type="text" name="frmSerCre" /> μmol/L
    </p>
   <p><label for="text4">Gender:</label>
      <select name="frmGender" id="select">
        <option value="M" >Male</option>
        <option value="F" >Female</option>
      </select>


    </p>
    <p><input type="submit" value="Calculate" /></p>
  </form>

  <p>@totalMessage</p>

some help im probably going about it in the completely wrong way!

Mr. Mr.
  • 4,257
  • 3
  • 27
  • 42
Sheep_child
  • 27
  • 2
  • 7

1 Answers1

1

on the line where is says:

totalMessage = "Total = " + total;

Try this instead:

totalMessage = "Total = " + total.ToString("0.00");

Let me know if that helps.

Goodluck

=======================================

Here is the code I have working on my machine:

@{

var total = 0m;

    var totalMessage = "";
    if (IsPost)
    {
        var age = Request["frmage"].AsInt();
        var weight = Request["frmweight"].AsDecimal();
        var SerCre = Request["frmSerCre"].AsDecimal();
        var sexfactor = Request["frmGender"].AsBool();
        total = Convert.ToDecimal(age*SerCre);
        totalMessage = "Total = " + total.ToString("0.00");
    }
}

<form method="post">
<p>
    <label for="text1">Age:</label>
    <input type="text" name="frmAge" size="3" />Years
</p>
<p>
    <label for="text2">Weight:</label>
    <input type="text" name="frmWeight" />in Kg (1st = 6.35kg)
</p>
<p>
    <label for="text3">Serum Creatinine:</label>
    <input type="text" name="frmSerCre" />
    μmol/L
</p>
<p>
    <label for="text4">Gender:</label>
    <select name="frmGender" id="select">
        <option value="M">Male</option>
        <option value="F">Female</option>
    </select>
</p>
<p>
    <input type="submit" value="Calculate" /></p>
</form>
<p>@totalMessage</p>
Mr. Mr.
  • 4,257
  • 3
  • 27
  • 42
  • that displays a decimal but doesn't let me add them up look below for what I have changed now! – Sheep_child Jul 05 '12 at 07:38
  • 1
    When you use your 'var' to create variables the compiler? (I think) tries to figure out what the type should be, so when you say: var total = 0 the compiler thinks you want an int, when in fact you want it to be a decimal, you should add an 'm' to the end of the preset value to distinguish it as a decimal. – Mr. Mr. Jul 05 '12 at 09:52
  • That's fantastic! that works now just to put the rest of the calcualtions in to make it come up with the right answer! what does the "m" stand for?? – Sheep_child Jul 05 '12 at 11:16
  • In this question (http://stackoverflow.com/questions/977484/what-does-the-m-stand-for-in-c-sharp-decimal-literal-notation) there is an accepted answer but I prefer to believe it means 'mantissa' :) – Mr. Mr. Jul 05 '12 at 11:38