I have an issue when rounding a variable in STATA
I use the following STATA function
gen Rounded_ Quantity = round(Quantity, 1.0)
I am presenting in the table below how it looks.
Sometimes when the value of the variable is not a decimal number (i.e. when it is an integer), the rounded value reduces by 1 or increases by 1 ( as shown in the results of ID 1 and ID 2). I don’t get the reason. I think in both cases, the values of Rounded_ Quantity is supposed to be the same as the values of Quantity. I am wondering why it is not the same.
However, for some integers, it still works perfectly. The value of the Rounded_ Quantity is the same as the values of Quantity as shown in the result for ID 3.
When the value of the variable is decimal, it works perfectly, as shown in the results for IDs 4 and 5.
Would you please suggest to me any clue why the rounded value of 1 and 2 changed this way? Would you offer me any solution for this, please?
Thank you, and looking forward to hearing from you,
I use the following STATA function
gen Rounded_ Quantity = round(Quantity, 1.0)
I am presenting in the table below how it looks.
Sometimes when the value of the variable is not a decimal number (i.e. when it is an integer), the rounded value reduces by 1 or increases by 1 ( as shown in the results of ID 1 and ID 2). I don’t get the reason. I think in both cases, the values of Rounded_ Quantity is supposed to be the same as the values of Quantity. I am wondering why it is not the same.
However, for some integers, it still works perfectly. The value of the Rounded_ Quantity is the same as the values of Quantity as shown in the result for ID 3.
When the value of the variable is decimal, it works perfectly, as shown in the results for IDs 4 and 5.
| ID | Quantity | Rounded_ Quantity= round(Quantity, 1.0) |
| 1 | 18169497 | 18169496 |
| 2 | 17356187 | 17356188 |
| 3 | 22467414 | 22467414 |
| 4 | 6472872.3 | 6472872 |
| 5 | 5513729.8 | 5513730 |
Thank you, and looking forward to hearing from you,

Comment