Sheep_child
Sheep_child

Reputation: 27

Decimal calculations

I have got a form into which information is entered, and a drop down box for one section, which is male or female. (which gives out a value depending on that 1.23 or 1.04) which i now have working thanks to the wonderful minds on here! but still have an issue with the calculations with these figures.

@{
  var total = 0m;
  var totalMessage = "";

    if (IsPost)
    {
        var age = Request["frmage"].AsInt(0);
        var weight = Request["frmweight"].AsDecimal();
        var SerCre = Request["frmSerCre"].AsDecimal();
        var sexfactor = Request["frmGender"];
        var sexValue = sexfactor == "M" ? 1.23 : 1.04;

        total = Convert.ToDecimal ((((140 - age)*weight)* sexValue )/SerCre) ;

        totalMessage = "Calculated creatinine clearance (ml/min)= " + total.ToString("0.00")+ sexValue ;
    }
}

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

do i need to convert the decimal answers so that the code can see it as a decimal? even though they are already!

Upvotes: 1

Views: 394

Answers (2)

Mr. Mr.
Mr. Mr.

Reputation: 4285

var sexValue = sexfactor == "M" ? 1.23 : 1.04;

Should be:

var sexValue = sexfactor == "M" ? 1.23m : 1.04m;

The 'm' after the two values makes it obvious that you are dealing with decimals.

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

Here is my version:

@{
    ViewBag.Title = "TestButton";


    var totalMessage = "";
    if (IsPost)
    {
        var age = Request["frmage"].AsInt();
        var weight = Request["frmweight"].AsDecimal();
        var SerCre = Request["frmSerCre"].AsDecimal();
        var sexfactor = Request["frmGender"];

        var sexValue = sexfactor == "M" ? 1.23m : 1.04m;

        var total = Convert.ToDecimal((((140 - age) * weight) * sexValue) / SerCre);
        totalMessage = "Calculated creatinine clearance (ml/min)= " + (total + sexValue);
    }
}

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

Upvotes: 0

Freeman
Freeman

Reputation: 5801

The best way to do it is to declare your 'sexValue' variable explicitly using decimal. Example: Decimal sexValue = sexfactor == "M" ? 1.23M : 1.04M; and also specify the M(million) so it will know for sure that it is a decimal value. I use var only when needed because declaring variables explicitly makes the code at least more human readable.

Upvotes: 1

Related Questions