I have started to learn C#. I have created a console program that converts Swedish kronor to USD.
I have set the conversion rate to that 1$ is equal to 13 SEK.
When I run my code I get the answer 0$ always.
public class övn5
{
//double Result;
double Dollar;
int DollarRate = 11;
//int Pund;
//int PundRate = 13;
public double SekToDollar(int Sek)
{
return Dollar = Sek / DollarRate;
}
public void ConvertCurrency()
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.WriteLine("Set Sek");
int Sek = (int)Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Dollar: {0}", SekToDollar((int)Sek));
}
}
and in my main:
class MainClass
{
public static void Main(string[] args)
{
övn5 o5 = new övn5();
o5.ConvertCurrency();
}
}
What have I missed in the above code?
Thanks :)
What have I missed in the above code?
you're expecting double from integer division you can simply fix it by:
public double SekToDollar(int Sek)
{
return (double)Sek / DollarRate;
}
keep in mind that you should use decimal
for financial values/calculations etc. as madreflection said
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.