The problem is: the result of 25 / 100
or 50 / 100
is 0
, in C# and similar languages (C, C++, ...).
Why?
In C#, when you divide an integer (int
, long
, etc.) by an integer, the decimal section is truncated - 0.25
becomes 0
.
So, there are some solutions:
1) Simply write 0.25
(or 0.5
) in your code:
if (dUIAnswer == "yes" || dUIAnswer == "ya")
{
quote += 0.25 * quote;
}
if (coverageType == "full coverage")
quote += 0.5 * quote; ;
return quote;
2) Convert one of the numebrs to double
or to float
, by attaching it the postfix D
(for double
) or F
(for float
), or by adding to it an .0
at the end (which does it a double
), or by casting it with (<type>)
:
if (dUIAnswer == "yes" || dUIAnswer == "ya")
{
quote += (25.0 / 100D) * quote;
}
if (coverageType == "full coverage")
quote += ((float)50 / 100) * quote; ;
return quote;
(You also can convert it to decimal
, explicitly or by attaching M
as postfix).