Caché ObjectScript Reference
$JUSTIFY
[Back] [Next]
   
Server:docs2
Instance:LATEST
User:UnknownUser
 
-
Go to:
Search:    

Right-aligns an expression within a specified width, rounding to a specified number of fractional digits.
Synopsis
$JUSTIFY(expression,width,decimal)
$J(expression,width,decimal)
Parameters
expression The value that is to be right-aligned. It can be a numeric value, a string literal, the name of a variable, or any valid Caché ObjectScript expression.
width The number of characters within which expression is to be right-aligned. A positive integer or an expression that evaluates to a positive integer.
decimal Optional — The number of fractional digits. A positive integer or an expression that evaluates to a positive integer. Caché rounds or pads the number of fractional digits in expression to this value. If you specify decimal, Caché treats expression as a numeric.
Description
$JUSTIFY returns the value specified by expression right-aligned within the specified width. You can include the decimal parameter to decimal-align numbers within width.
$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 American-format locales, or a comma (,) for European-format 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 right-aligned so that the DecimalSeparator characters align in a column of numbers. $JUSTIFY is especially useful for outputting formatted values using the WRITE command.
Parameters
expression
The value to be right-justified, and optionally expressed as a numeric with a specified number of fractional digits.
width
The width in which to right-justify the converted expression. If width is greater than the length of expression (after numeric and fractional digit conversion), Caché right-justifies to width, left-padding 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.
decimal
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.
The $DOUBLE values INF, -INF, and NAN are returned unchanged by $JUSTIFY, regardless of the decimal value.
$JUSTIFY and $FNUMBER
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:
Therefore, to properly add NumericGroupSeparators, round fractional digits, append a currency symbol, and right-align the resulting number, you use $FNUMBER to perform rounding and inserting of NumericGroupSeparators. You then use $JUSTIFY with 2-parameter syntax to right-align the resulting string:
  SET num=123456.789
  SET fmtnum=$FNUMBER(num,",",2)
  SET money="$"_fmtnum
  SET rmoney=$JUSTIFY(money,15)
  WRITE ">",rmoney,"<"
 
Examples
The following example performs right-justification 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 right-justification 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 right-justification 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(x-x)
  WRITE !,"Double INF minus INF: ",y
  WRITE !,">",$JUSTIFY(y,12,2),"<"
 
See Also