0

My code is here

    if (dUIAnswer == "yes" || dUIAnswer == "ya")
    {
        quote += (25 / 100) * quote;            
    }

    if (coverageType == "full coverage")
        quote += (50 / 100) * quote; ;


   return quote;

I am basically trying to increase the value "quote" by 25% of itself if the user has a DUI. Please give me a simple solution of code that is possible.

Jeffrey Padgett
  • 218
  • 1
  • 14
  • What is the type of `quote`? – Yeldar Kurmangaliyev Nov 19 '18 at 11:57
  • 4
    What's not working about this code? – Mark Nov 19 '18 at 11:57
  • A decimal type, it is a variable of type decimal. – Jeffrey Padgett Nov 19 '18 at 11:58
  • 2
    @JeffreyPadgett The problem in your code is that you actually do an integer division, so `(25 / 100)` results to 0, and the whole sentence on the right side results to 0. Just do `quote *= 1.25`. – Yeldar Kurmangaliyev Nov 19 '18 at 11:58
  • The value remains the same for some reason. Maybe the IF statement is not detecting the string value of DUI answer? I’m not sure. I’ll double check in debug mode. – Jeffrey Padgett Nov 19 '18 at 11:59
  • 1
    `quote += (25M / 100M) * quote;` – Renatas M. Nov 19 '18 at 11:59
  • Thank you so much, absolutely correct. I appreciate it so much. – Jeffrey Padgett Nov 19 '18 at 12:02
  • @JeffreyPadgett when you were asked what is not working about the code, they didn't mean what is the cause of it not working. They meant what is the behavior you observe that doesn't match to the expected behavior. A good, clear question should be alone the lines: "I tried this. I expect this, but instead this happens" You haven't mention in your question what is the problem with the code i.e. what is it's behavior. – bolov Nov 19 '18 at 12:08
  • Thank you very much @bolov .. This was one of my very first StackOverflow questions and I will do better next time. – Jeffrey Padgett Nov 19 '18 at 12:09
  • Also worth noting is that _quote += quote * 25 / 100;_ would also work, with the first value in the expression being a decimal the compiler will do it's best to convert other values to appropriate types - with the brackets there the integer value would be calculated before type conversion. – PaulF Nov 19 '18 at 12:21

3 Answers3

3

25 / 100 does integer division so the result is 0 (of type int). Make them decimal i.e. 25.0M / 100.0M or better yet:

quote *= 1.25M;
bolov
  • 72,283
  • 15
  • 145
  • 224
0

To add 25% to the cyrrent value so it will become 125% of the current value, so multipy it by 1.25 (125%):

if (dUIAnswer == "yes" || dUIAnswer == "ya")
        quote *= 1.25M;         

    if (coverageType == "full coverage")
        quote *= 1.5M;
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
0

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

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77