This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
How can I divide two integers to get a double?
(9 answers)
Closed 2 years ago.
I'm currently making a new project on c# and i have a problem - calculations are wrong. My calculation is 1 * (1 + (1 - 160 / 180)) but it returns 2. I checked this in my calculator and the right answer is 1.11111...
Please help!
P. S. sorry for my bad english
160 / 180 is integer division and will result in 0.
Try using 160.0/180 in order to promote the result a double.
Related
This question already has answers here:
Division returns zero
(8 answers)
Closed 9 months ago.
score = houseWithHospitalCount/houseCount * houseCount;
score is a float and the rest are int.
when houseWithHospitalCount is less than houseCount score always returns 0
example: 2 = 2/2 * 2
example: 0 = 1/2 * 2
Your question is similar to this question, answered here:
Division returns zero
From looking at that post, it appears you would need to use decimal literals for your ints to be interpreted as decimals and the division to not return zero.
This question already has answers here:
Possible Loss of Fraction
(5 answers)
Closed 4 years ago.
I'm trying to print out this integer variable which in the case of my test numbers should be 5,2333 but only 5 gets printed.
int finalTimeInt = ((N * 60 + n) / 2) / 60;
Console.Writeline(finalTimeInt);
As stated already I'm expecting an output of 5,2333 but am only getting a 5.
Integers can't hold decimals. If you want a variable to store decimals, you should be using a floating-point type, like float, or double if you want more accuracy.
This question already has answers here:
How do I display a decimal value to 2 decimal places?
(19 answers)
Closed 5 years ago.
so I have the following problem I'm trying to output 2 numbers after the decimal comma but it doesn't output it with 2 numbers only when I have zero or more than one zero at the end of the number.
Console.WriteLine(Math.Round(s * 0.07, 2));
here's how I did it.
You might want:
Console.WriteLine($"{s:N2}");
This question already has answers here:
How to limit a decimal number? [duplicate]
(6 answers)
Closed 6 years ago.
So,
I got example number: 0.00857382942837523 which length is 18, but I would like to remove from the end example 9 numbers. Thing is, that number is different every time, so I can't "predict" numbers what are going to be at the end.
//My question was marked to duplicate, but it can't be solved like that. Mine has 0.00, if I would do math.round it would give up 0...
You need Math.Truncate
decimal d= 0.00857382942837523m;
d= Math.Truncate(d * 1000000000m) / 1000000000m;
Console.WriteLine(d); // 0.008573829
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
c# - How do I round a decimal value to 2 decimal places (for output on a page)
I have a number
long n = 32432432423;
I want to divide this by 1450 and print on a console with 2 decimal places (rounded)
How can I do it?
COnsole.WriteLine(????);
Console.WriteLine("{0:N2}", ((double)n) / 1450);
Console.WriteLine("{0:0.00}", 32432432423 / 1450.0);
make use of Math.Round
Console.WriteLine( Math.Round(Convert.ToDecimal(32432432423 / 1450.0), 2));
Result: 22367194.77