Reputation: 63
Here is a simple calculator, to calculate how much candy you can get when dividing the kilogram price with the money you have allocated for candy.
In example: "Candy costs 5$ a kilo. I have 3.50$ allocated for candy. I would use this program to calculate the amount of candy I get."
It works fine until using decimals. I want to convert the string to Double so I can use decimals with the calculator. 4$ kilo price and 8$ money of course results to 2 kilos of candy. But if the kilo price was 4.80$ and I had 9.30$ money, I would only get an error and cause the command prompt to crash.
static void Main(string[] args)
{
//The variables
int int1;
int int2;
string text1;
string text2;
//The actual calculation
System.Console.Write("Please input the kilogram price of candy: ");
text1 = System.Console.ReadLine();
int1 = int.Parse(text1);
System.Console.Write("Please input the money allocated for candy ");
text2 = System.Console.ReadLine();
int2 = int.Parse(text2);
System.Console.WriteLine("With the amount of money you input you would get " + (int2 / int1) + " kilos of candy.");
}
}
}
Upvotes: 1
Views: 4082
Reputation: 598
As to your question:
Just use doubles decimals in the first place!
decimal price;
decimal allowance;
Also, some additional unsolicited advice:
You've got a few issues here.
First of all, you'll need to swap your division in the WriteLine
statement to get the correct calculation.
Also, I'd recommend renaming your variables to something more descriptive.
Here's your code with some modifications. I've commented each change.
//The variables
decimal price; //CHANGE: descriptive variables!
decimal allowance;
string priceInput;
string allowanceInput;
//The actual calculation
System.Console.Write("Please input the kilogram price of candy: ");
priceInput = System.Console.ReadLine();
price = decimal.Parse(priceInput); //CHANGE: use double.Parse here instead of int.Parse
System.Console.Write("Please input the money allocated for candy ");
allowanceInput = System.Console.ReadLine();
allowance = decimal.Parse(allowanceInput); //CHANGE: use double.Parse here instead of int.Parse
System.Console.WriteLine("With the amount of money you input you would get "
+ Math.Round(allowance / price, 2) + " kilos of candy.");
//CHANGE: Divide the money you have by the amount it costs to get the KG of candy.
EDIT: Added decimals due to working with money.
Upvotes: 1
Reputation: 35260
You need to parse your inputs from decimal
, not int
. Here, try it this way:
System.Console.Write("Please input the kilogram price of candy: ");
decimal pricePerKilo = decimal.Parse(System.Console.ReadLine());
System.Console.Write("Please input the money allocated for candy ");
decimal amountAllocatedForCandy = decimal.Parse(System.Console.ReadLine());
System.Console.WriteLine("With the amount of money you input you would get " + (amountAllocatedForCandy / pricePerKilo) + " kilos of candy.");
System.Console.Read();
An int
is a type that can only store whole numbers -- i.e. no fractional component/decimal digits -- which is why you got an error when you tried to parse a string number with decimal digits into an int
. A decimal
, on the other hand, is used to store numbers with fractional components.
Each type has its uses: you would never want to store, say, number of candy pieces using a decimal
since that can only be a whole number. Doing so could lead to unnecessary confusion for future maintainers of your code and could lead to bugs that are hard to find.
Upvotes: 3
Reputation: 151588
You're using int
variable types, which can only contain integer numbers, i.e. "whole" numbers without decimals, such as 1
and 42
. As soon as you want to use decimals, you'll need a variable type that can support this.
C# has a few of those built in: float
, double
and decimal
. Given you're working with monetary data, where precision and accuracy are very important, you must use decimal
.
You also want to use the bool
-returning TryParse()
as opposed to the exception-throwing Parse()
method.
So change your code to use decimal
and decimal.TryParse()
:
decimal pricePerKilogram;
string input = Console.ReadLine();
if (!decimal.TryParse(input, out pricePerKilogram))
{
// show error or retry
}
Upvotes: 5
Reputation: 3326
that is because 4.80 or 9.30 is not integer.
double double1;
double double2;
string text1;
string text2;
//The actual calculation
System.Console.Write("Please input the kilogram price of candy: ");
text1 = System.Console.ReadLine();
double1 = double.Parse(text1);
System.Console.Write("Please input the money allocated for candy ");
text2 = System.Console.ReadLine();
double2 = double.Parse(text2);
System.Console.WriteLine("With the amount of money you input you would get " + (double2 / double1) + " kilos of candy.");
NOTE: INT stores integer value like 1 2 3 4 etc...
Upvotes: 1