Right-aligns an expression within a specified width, rounding to a specified number of fractional digits.
||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 ObjectScript expression.
||The number of characters within which expression is to be right-aligned. A positive integer or an expression that evaluates to a positive integer.
||Optional The number of fractional digits. A positive integer or an expression that evaluates to a positive integer. InterSystems IRIS rounds or pads the number of fractional digits in expression to this value. If you specify decimal, InterSystems IRIS treats expression as a numeric.
$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:
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.
The value to be right-justified, and optionally expressed as a numeric with a specified number of fractional digits.
If string justification is desired, do not specify decimal. The expression can contain any characters. $JUSTIFY right-justifies expression, as described in width.
If numeric justification is desired, specify decimal
. If decimal
is specified, $JUSTIFY
to a canonical number
. It resolves leading plus and minus signs and removes leading and trailing zeros. It truncates expression
at the first nonnumeric character. If expression
begins with a nonnumeric character (such as a currency symbol), $JUSTIFY
converts the expression
value to 0. For further details on how InterSystems IRIS converts a numeric to a canonical number, and InterSystems IRIS handling of a numeric string containing nonnumeric characters, refer to the Numbers
section of the Data Types and Values chapter of Using ObjectScript
After $JUSTIFY converts expression to a canonical number, it zero-pads or rounds this canonical number to decimal number of fractional digits, then right-justifies 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 right-justify the converted expression. If width is greater than the length of expression (after numeric and fractional digit conversion), InterSystems IRIS 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), InterSystems IRIS 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 InterSystems IRIS 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.
values INF, -INF, and NAN are returned unchanged by $JUSTIFY
, regardless of the decimal
$JUSTIFY and $FNUMBER
You can use $FNUMBER
to format a number for display. Both $JUSTIFY
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 right-aligned 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 prepended a currency symbol. ($JUSTIFY interprets NumericGroupSeparators or currency symbols as nonnumeric characters.)
Therefore, to properly add NumericGroupSeparators, round fractional digits, prepend 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:
The following example performs right-justification on strings. No numeric conversion is performed:
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
return the following lines:
The following example performs numeric right-justification with the $DOUBLE values INF and NAN:
WRITE !,"Double: ",x
WRITE !,"Double INF minus INF: ",y
Content Date/Time: 2019-08-23 06:48:00