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
Bootstrapped SEs for quantile regressionHi, I have a standard difference-in-differences setup where a reform is introduced in one region in…
Help remarking duplicates in household surveyHello everyone, I have a simple question, which I have failed to figure out. How can I remark split …
xtscc: remove constant term from differenced estimationHello, I am estimating a first difference model of this kind: d.y= d.x1 + d.x2 + d_t*. where x1 and …
How to measure time dependent reproductive number (Rt) in stataHello everyone. I'm trying to make epidemiological curve of COVID 19 in my region. I wonder if STATA…
Mixlogit with 'corr' option: convergence issueDear all, I am encountering a convergence problem with the 'mixlogit' command (written by Arne Risa…
Subscribe to:
Post Comments (Atom)
0 Response to Rounding with a macro
Post a Comment