On Thu, 13 Jun 2013, Summers, Peter wrote:
I'm trying to generate a matrix of "dates" that I can
use on
the x-axis of graphs of forecasts. I want the format to be
"yyyy.q" or "yyyy.mm" for quarterly or monthly data,
respectively. Things work fine for quarterly data, but
something weird happens with monthly data.
I create a string with the desired format, then make the
corresponding matrix entry using string substitution. So for
example the string "2009.04" should just become the "number"
2009.04. However, the string substitution results in the
second "decimal" being truncated, so I get 2009.0 instead of
2009.04, or 2009.1 instead of 2009.12.
The attached script illustrates the problem.
Hmm, I'm not seeing a problem here (current CVS on Linux). But
note that the recommended way to get a month to appear in the
desired format with sprintf is to use the conversion "%02d"
(which inserts a zero if and only if it's needed).
If I do
<hansl>
scalar yr = 2009
scalar mo = 4
string s
sprintf s, "%d.%02d", yr, mo
printf "s = '%s'\n", s
scalar d = @s
</hansl>
I get:
<output>
? scalar yr = 2009
Generated scalar yr = 2009
? scalar mo = 4
Generated scalar mo = 4
? string s
? sprintf s, "%d.%02d", yr, mo
s = '2009.04'
? scalar d = 2009.04
Generated scalar d = 2009.04
</output>
Allin Cottrell