
Caché ObjectScript Reference
$JUSTIFY



Server:docs2 
Instance:LATEST 
User:UnknownUser 


Rightaligns an expression within a specified width, rounding to a specified number of fractional digits.
Synopsis
$JUSTIFY(expression,width,decimal)
$J(expression,width,decimal)
$JUSTIFY recognizes the DecimalSeparator character for the current locale. It adds or deletes a DecimalSeparator character as needed. The DecimalSeparator character depends upon the locale; commonly it is either a period (.) for Americanformat locales, or a comma (,) for Europeanformat locales. To determine the DecimalSeparator character for your locale, invoke the following method:
WRITE ##class(%SYS.NLS.Format).GetFormatItem("DecimalSeparator")
Commonly,
$JUSTIFY is used to format numbers with fractional digits: every number is given the same number of fractional digits, and the numbers are rightaligned so that the DecimalSeparator characters align in a column of numbers.
$JUSTIFY is especially useful for outputting formatted values using the
WRITE command.
The value to be rightjustified, and optionally expressed as a numeric with a specified number of fractional digits.


After
$JUSTIFY converts
expression to a canonical number, it zeropads or rounds this canonical number to
decimal number of fractional digits, then rightjustifies the result, as described in
width.
$JUSTIFY does not recognize NumericGroupSeparator characters, currency symbols, multiple DecimalSeparator characters, or trailing plus or minus signs.
The
width in which to rightjustify the converted
expression. If
width is greater than the length of
expression (after numeric and fractional digit conversion), Caché rightjustifies to width, leftpadding as needed with blank spaces. If
width is less than the length of
expression (after numeric and fractional digit conversion), Caché sets
width to the length of the
expression value.
Specify
width as a positive integer. A
width value of 0, the null string (""), or a nonnumeric string is treated as a
width of 0, which means that Caché sets
width to the length of the
expression value.
The number of fractional digits. If
expression contains more fractional digits,
$JUSTIFY rounds the fractional portion to this number of fractional digits. If
expression contains fewer fractional digits,
$JUSTIFY pads the fractional portion with zeros to this number of fractional digits, adding a Decimal Separator character, if needed. If
decimal=0,
$JUSTIFY rounds
expression to an integer value and deletes the Decimal Separator character.
If the
expression value is less than 1,
$JUSTIFY inserts a leading zero before the DecimalSeparator character.
You can use
$FNUMBER to format a number for display. Both
$JUSTIFY and
$FNUMBER can round (or zero pad) to a specified number of fractional digits.
$FNUMBER can also be used to add NumericGroupSeparator characters. However, note the following:

$FNUMBER cannot format a number once it has been rightaligned using
$JUSTIFY. (
$FNUMBER interprets the leading spaces as nonnumeric characters.)

$JUSTIFY cannot perform numeric justification on a number once you have added NumericGroupSeparator characters or have appended a currency symbol. (
$JUSTIFY interprets NumericGroupSeparators or currency symbols as nonnumeric characters.)
Therefore, to properly add NumericGroupSeparators, round fractional digits, append a currency symbol, and rightalign the resulting number, you use
$FNUMBER to perform rounding and inserting of NumericGroupSeparators. You then use
$JUSTIFY with 2parameter syntax to rightalign the resulting string:
SET num=123456.789
SET fmtnum=$FNUMBER(num,",",2)
SET money="$"_fmtnum
SET rmoney=$JUSTIFY(money,15)
WRITE ">",rmoney,"<"
The following example performs rightjustification on strings. No numeric conversion is performed:
WRITE ">",$JUSTIFY("right",10),"<",!
WRITE ">",$JUSTIFY("aligned",10),"<",!
WRITE ">",$JUSTIFY("+0123.456",10),"<",!
WRITE ">",$JUSTIFY("string longer than width",10),"<",!
The following example performs numeric rightjustification with a specified number of fractional digits:
SET var1 = 250.50999
SET var2 = 875
WRITE !,$JUSTIFY(var1,20,2),!,$JUSTIFY(var2,20,2)
WRITE !,$JUSTIFY("_________",20)
WRITE !,$JUSTIFY("TOTAL",9),$JUSTIFY(var1+var2,11,2)
return the following lines:
250.51
875.00
_______
TOTAL 1125.51
The following example performs numeric rightjustification with the
$DOUBLE values INF and NAN:
SET rtn=##class(%SYSTEM.Process).IEEEError(0)
SET x=$DOUBLE(1.2e500)
WRITE !,"Double: ",x
WRITE !,">",$JUSTIFY(x,12,2),"<"
SET y=$DOUBLE(xx)
WRITE !,"Double INF minus INF: ",y
WRITE !,">",$JUSTIFY(y,12,2),"<"