diff --git a/doc/src/Section_commands.txt b/doc/src/Section_commands.txt index 3dabdbeaa1e1091db35b798f19cf9f3b2b74848f..1835d0505f3ba452d7fe96ff907743d309ee080e 100644 --- a/doc/src/Section_commands.txt +++ b/doc/src/Section_commands.txt @@ -129,6 +129,15 @@ region 1 block $((xlo+xhi)/2+sqrt(v_area)) 2 INF INF EDGE EDGE :pre so that you do not have to define (or discard) a temporary variable X. +Additionally, the "immediate" expression may be followed by a colon, +followed by a C-style format string, e.g. "%f" or "%.10g", which must be +appropriate for formatting a double-precision floating-point value. The +format string will be used to output the result of the variable evaluation, +so you do not have to define a temporary "format-style variable"_variable.html. +This may be used for formatting print output: + +print "Final energy per atom: $(pe/atoms:%10.3f) eV/atom" :pre + Note that neither the curly-bracket or immediate form of variables can contain nested $ characters for other variables to substitute for. Thus you cannot do this: diff --git a/src/input.cpp b/src/input.cpp index b83a3683e4a3eae3984e842f55f33e672471fd85..f2afa342872f4af3c2b38f1c740229a2dd8624cf 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -516,7 +516,14 @@ void Input::substitute(char *&str, char *&str2, int &max, int &max2, int flag) if (var[i] == '\0') error->one(FLERR,"Invalid immediate variable"); var[i] = '\0'; beyond = ptr + strlen(var) + 3; - sprintf(immediate,"%.20g",variable->compute_equal(var)); + // check if an inline format specifier was given + char fmtstr[64] = "%.20g"; + char *fmtflag; + if ((fmtflag=strrchr(var, ':')) && (fmtflag[1]=='%')) { + strncpy(fmtstr,&fmtflag[1],sizeof(fmtstr)-1); + *fmtflag='\0'; + } + sprintf(immediate,fmtstr,variable->compute_equal(var)); value = immediate; // single character variable name, e.g. $a