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
Odd scalar behaviourDear statalisters, I am running the following code to cut up a very large file (>15g) into small…
Running regression while controlling for industry and yearsI'm investigating the influence of patenting (pat_yr -> patents per year) on the returnoninvestem…
merge 1:m using new_id, overwrites id in masterfileDear listers I have two files that i want to merge. File 1 includes: id (string), case_id (string)…
Correlation between dummy variablesI am working on STATA 14 and have a panel data- long view with cross sectional data of Indian non-fi…
Creating 2 dummy variables in StataHi everyone I am back with another question to help me out I have a table output containing lots o…
Subscribe to:
Post Comments (Atom)
0 Response to Rounding with a macro
Post a Comment