How do I align right exploration result NumtoStr

@beppe, could you help me to get this done.

Do you mean something like this ?

Still not very clear, but by default All Text is Left Aligned and Numeric Types are right aligned.
You want to swap it ? As in all text right aligned.

Will check some code out but you might have to pad the text yourself with a small helper function.

Edit:For courtesy sake, can you post your test code for NumToStr() that you're trying with ?

@RUPEEXN it is a lot better to use numbers directly in explorations since they are already right aligned.

If for any strange reason1 you need to use strings (derived from numbers) to produce some fixed spaces text output, you can try to employ the StrFormat( formatstr, ... ) function like in this example:

n1 = 12.56;
n2 = 1245.5;
n3 = 482.75;
n4 = 100;
// Using StrFormat() to convert a number to a formatted string (no new line char added)
s1 = StrFormat( "%15.2f", n1 );
s2 = StrFormat( "%15.2f", n2 );
s3 = StrFormat( "%15.2f", n3 );
s4 = StrFormat( "%15.2f", n4 );
//      123456789012345
printf( "===============" + "\n" ); // "\n" is added to get multiple lines using printf
printf( s1 + "\n" );
printf( s2 + "\n" );
printf( s3 + "\n" );
printf( s4 + "\n" );

// Normally you will use printf directly (appending \n to the formatstr)
printf( "%15.2f\n", n1 );
printf( "%15.2f\n", n2 );
printf( "%15.2f\n", n3 );
printf( "%15.2f\n", n4 );

// In general, StrFormat () is better used to create strings embedding multiple number or strings
// Title = StrFormat("%s (%s) - Open %g, Hi %g, Lo %g, Close %g", Name(),  FullName(), O, H, L, C);
// _TRACE(Title);
// or 
// _TRACEF("%s (%s) - Open %g, Hi %g, Lo %g, Close %g", Name(),  FullName(), O, H, L, C);

Partial output displayed using a monospaced font (like Lucida Consolas, Courier New)


If you display this output using a normal font, the result is far from optimal....

In my example, the length of the resulting strings is 15 chars (including the decimal point and the 2 decimals) with a variable number of spaces before the numbers.

This is achieved using the StrFormat() function that allows specifying how the resulting string will be formatted, using a list of arguments that will be inserted into the "formatstr" in places, where %f, %g or %e specifications are entered (%s is supported from version 6.20).

In the above snippet, my "formastr" is only a single float number specification


and it is composed as follow:

%" : is the mandatory char used to indicate where an argument will be inserted in the string
15 : width of the resulting string
. : a decimal point indicator
2 : the decimal part precision
f : char to indicate that argument to be inserted is a float number; this also teminate the spcification

The width (15) is the minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger (the string will be wider in this case)
The precision (2) specifies the number of digits to be printed after the decimal point.

As showed in the example Printf() is closely related to StrFormat(): they behave identically except that printf writes output to the window, while StrFormat does not write anything to output window but returns resulting string instead (see also _TraceF()).

For additional examples, please, see the last part of this KB article (where FormatStr() is used to create the default chart title).

1) For example to create old-style report files that were printed with dot matrix printers - is there still someone who uses them?


Very nice explanation @beppe :clap:

Just another add-on to a code snippet that I use in chart to control the precision depending on Price value.

If its useful to others. I use it in

_N(Title = … // Space constraint

where line spacing is a constraint especially if multiple charts are tiled.

function zz( mys ) {
	return	mys = 
		WriteIf( mys > 4999, NumToStr(mys, 10.0, False), 
		WriteIf( mys > 999, NumToStr(mys, 10.1, False),
		WriteIf( mys > 5, NumToStr(mys, 10.2, False),
		NumToStr( mys, 10.3, False) )));

With this the OHLC value remain fixed visual when scrolling multiple tickers which otherwise would keep bouncing left or right (visually).

_N(Title = zz(O) + " O " + zz(H) + " H " + zz(L) + " L " + zz(C) + " C " );

working example


Thank you, @beppe.
I will try this and give you feedback

@travick, thank you for your suggestions. I will keep in mind and apply.