Yanping Chen raised a question in the Chinese COS forum on the output of Eviews: how to (re)format the decimal coefficients in equations as text output? For example, we want to round the numbers in CC = 16.5547557654 + 0.0173022117998*PP + 0.216234040485 * PP(-1) + 0.810182697599 * (WP + WG) to the 3rd decimal places. This can be simply done by regular expressions, as decimals always begin with a period (.). The basic steps are:

find out where are the decimals in the character string;

format them;

replace the original decimals with formatted values;

Given a character vector, we can format the decimals with the code below:

I used sapply() for 3 times to avoid explicit loops but consequently the code might be difficult to read. The critical part is the regular expression \\.[0-9]+ which means one of more (controlled by + after [0-9]) digits ([0-9] or [:digit:]) after a decimal point .. As . is a metacharacter in regular expressions, we need to use a backslash before it, and again, \ is a special character in R, so we need another backslash to denote a backslash. o:-)