I declare the variable as Decimal and use division operator / but I always get round numbers on my VS CODE.

For example :

Decimal age = 225/12;

gives the output 18


1 Answer 1


You are performing division on Integer values, so the result is an Integer before you assign it to a Decimal variable. This behavior is documented here:

Loss of Fractions in Divisions

When dividing numeric Integer or Long values, the fractional portion of the result, if any, is removed before performing any implicit conversions to a Double or Decimal. For example, Double d = 5/3; returns 1.0 because the actual result (1.666...) is an Integer and is rounded to 1 before being implicitly converted to a Double. To preserve the fractional value, ensure that you are using Double or Decimal numeric values in the division. For example, Double d = 5.0/3.0; returns 1.6666666666666667 because 5.0 and 3.0 represent Double values, which results in the quotient being a Double as well and no fractional value is lost.

Express your values as a Double or assign them to a Decimal before performing division to avoid this truncation behavior.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .