Hi,
To display a number rounded down to a user selected number of decimal places, I wrote some code that gave a surprising answer.
Attempt 1
local x = 0.20629048
local dp = 4
local x_round = round(`x', 10^-`dp')
display "this is x `x' this is x_round `x_round'"
The final line results in the following unexpected output - this is x .20629048 this is x_round .2063000000000001
From the following you would expect x_round above to be calculated as .2063
local x = 0.20629048
local dp = 4
display 10^-`dp'
display round(`x', .0001)
However, I can get around the problem with creating a new macro that evaluates the number of decimal places
Attempt 2
local x = 0.20629048
local dp = 4
local dp_value = 10^-`dp'
local x_round = round(`x', `dp_value')
display "this is x `x' this is x_round `x_round'"
The final line results in the following correct output - this is x .20629048 this is x_round .2063
Why does Attempt 1 fail?
Thanks for taking the time to consider this,
Don Vicendese
Related Posts with Rounding with a macro
Nbreg with i.id for fixed effects not convergingHi, I have a panel dataset consisting of 3,617 IDs where each ID has about 9-10 observations for it…
How to find upward variable bias?Dear Statalisters, I am currently running some analysis on a few different dependent variables in a…
Difference-in-difference with three time periodsDear all, I am working on my bachelor essay and I have to use stata for a regression. My topic is t…
Heteroscedasticity vs Homoscedasticity with different measures of dependent variableHi All, I am carrying out research for my bachelor thesis looking at the effect of gin consumption …
declare survey design for datasetHello, I have a question regarding how to correctly declare survey design for a dataset I am analyz…
Subscribe to:
Post Comments (Atom)
0 Response to Rounding with a macro
Post a Comment