C# calculation errors

1605 views c#

I have a c# code which calculates (304/1000)*100, which equals 30.4, however when I run the code in c# every single time I get 0.

decimal width = (maxSize.Width/ maxValue) * value; //304, 1000, 100 respectively
Console.WriteLine(width); //Returns 0

I don't understand what the hell is going on?

answered question

Hint: what is the smallest integer that can represent 304/1000?

@JörgWMittag, asking for the "closest" integer might be misleading, as any fraction in the division result is just cut off (like, the closest integer that would represent 9/10 would be 1)

2 Answers


Your input variables are all of type int. Cast any of them into decimal or suffix the number with the letter M to convert to a decimal before the calculation is performed.

posted this

Use correct type

maxSize.Width = 304m;
maxValue = 1000m;

decimal width = (maxSize.Width/ maxValue) * value;

posted this

Have an answer?


Please login first before posting an answer.