0

After division when storing result in float,double,decimal it gives result as 0. How can i store it.

decimal days = 0.00m;
days= 110 / 480;

Actual result is 0.2291666666666667 but i'm getting 0 in variable days. i have tries data types float,double & decimal.

Nishant
  • 35
  • 1
  • 8

1 Answers1

1

This will give you correct result :

decimal days = 0.00m;
days= 110m / 480m;

Explanation : To represent a decimal value 'm' is suffixed

 110 is Int32
 110m is Decimal

As you have defined days as decimal, values used in the calculation should also be decimal. Hope this helps to understand the value representation.

As @dumetrulo pointed out : 110 & 480 both are integers, 110 / 480 after integer division gives result 0; this result is then implicitly cast to decimal and stays 0.

boop_the_snoot
  • 3,209
  • 4
  • 33
  • 44