3

I am new to C# and this code always returns 0.00 and I don't know what is the reason. Could anyone help? It is a console program and a possible input is

sofia
1500

the result should be 120.00

using System;

namespace TradeComissions
{
    class TradeComissions
    {
        static void Main(string[] args)
        {
            var town = Console.ReadLine().ToLower();
            var amount = double.Parse(Console.ReadLine());
            double result = 0.0;

            if(town == "sofia")
            {
                if (amount >= 0 && amount <= 500) 
                {                 
                    result = amount * (5 / 100);    
                }
                else if (amount >= 500 && amount <= 1000) 
                {         
                    result = amount * (7 / 100);    
                }
                else if (amount >= 1000 && amount <= 10000) 
                {       
                    result = amount * (8 / 100);    
                }
                else if (amount > 10000) 
                {                          
                    result = amount * (12 / 100);   
                }
            }

            Console.WriteLine("{0:f2}", result);
        }
    }
}
Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
user3628807
  • 325
  • 3
  • 12

2 Answers2

4

You are doing a mathematical division between 2 integers where denom is bigger that numerator e.g. 5 / 100, that operation result into an integer too (zero), do instead:

result = amount * (5.0 / 100);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

This will work:

static void Main(string[] args)
{
    var town = Console.ReadLine().ToLower();
    var amount = double.Parse(Console.ReadLine());
    double result = 0.0;

    if (town == "sofia")
    {
        if (amount >= 0 && amount <= 500)
        {
            result = amount * (0.05);
        }
        else if (amount >= 500 && amount <= 1000)
        {
            result = amount * (0.07);
        }
        else if (amount >= 1000 && amount <= 10000)
        {
            result = amount * (0.08);
        }
        else if (amount > 10000)
        {
            result = amount * (0.12);
        }
    }
    double f = result;

    Console.WriteLine("{0:f2}", result);
    Console.ReadKey();
}
Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
Didu
  • 79
  • 2
  • 11