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
"Standalone" Arellano-Bond autocorrelation test and Test for heteroscedasticity when using gmmHello everyone, I am new to the list and I hope I am able to keep the rules of good conduct here. I …
XTIVREG: Fixed-effect model with IV robust or non-robust? IV choice and normal se assumptions for FEI am currently working on my thesis on the impact of land title certificate on crop diversity index …
convert US 1990 census occupation codes to SEI Stata syntaxHello, I am converting US 1990 census occupation codes to SEI. I was wondering if anyone could share…
xtscc error: "too many values r(134)";Hi, I am currently trying to estimate a Dynamic Panel Data Model (including a first-order lag of the…
using gammap and gammafit to estimate moments based probability and cumulative density functionsHi all, I have been trying to estimate household-and-period-specific conditional well-being probabil…
Subscribe to:
Post Comments (Atom)
0 Response to Rounding with a macro
Post a Comment