script utility functions formatInt(), formatFloat(), formatString()
by Orion Elenzil · 08/27/2007 (1:45 pm) · 1 comments
printf formatting can do some pretty cool stuff, like left-align a float in a field of 20 characters with 3 decimal places, print numbers in Hex, etc. sometimes it's nice to have that sort of formatting available in script.
here are three routines to provide that.
unfortantely they only take one parameter at a time, and also there's three of them.
i can see a few ways to consolidate them to be more like printf() itself,
but it's more work than i think is really called for. usually in script i've found that the default formatting is fine for most things, it's just the occasional single value which cries for more.
somewhere in consoleFunctions.cc, add these line:
examples:
here are three routines to provide that.
unfortantely they only take one parameter at a time, and also there's three of them.
i can see a few ways to consolidate them to be more like printf() itself,
but it's more work than i think is really called for. usually in script i've found that the default formatting is fine for most things, it's just the occasional single value which cries for more.
somewhere in consoleFunctions.cc, add these line:
ConsoleFunction(formatInt , const char*, 3, 3, "formatInt(format, int)")
{
char* ret = Con::getReturnBuffer(64);
dSprintf(ret, 64, argv[1], dAtoi(argv[2]));
return ret;
}
ConsoleFunction(formatFloat , const char*, 3, 3, "formatFloat(format, float)")
{
char* ret = Con::getReturnBuffer(64);
dSprintf(ret, 64, argv[1], dAtof(argv[2]));
return ret;
}
ConsoleFunction(formatString, const char*, 3, 3, "formatString(format, string)")
{
char* ret = Con::getReturnBuffer(4096);
dSprintf(ret, 4096, argv[1], argv[2]);
return ret;
}examples:
echo(formatInt("%0.4d", 37));
0037% echo(formatInt("0x%0.8X", 12345678));
0x00BC614EAbout the author

Torque Owner Deozaan