2.3.2.2.3 $( ) Substitution - Numeric to String Conversion

Syntax

The $() notation is used for numeric to string conversion. This notation evaluates the given expression at run-time, converts the result to a numeric string, and then substitutes the string for itself.

The notation has the following form:

$(expression [, format])

where the format is optional or can be Origin format, C-language format, or combined Origin and C-Language format as described in the following sections.

Expression can be one of the following:

Expression Format Example Description
number NA
type $(4.567); //ANS: 4.567
Display the number with up to 14 significant digits
numeric variable .3
xx=1.2345678; 
type $(xx, .3); //ANS: 1.235
Display the variable xx with 3 decimal places
numeric object property NA
//if a 2-column workbook is active
type "there are $(wks.ncols) columns";
//ANS: there are 2 columns
Display wks.ncols value
dataset variable NA
dataset ds1={2,4,6}; //dataset variable
type $(ds1); //ANS: 2 4 6
Display ds1 with space separated list
range variable *3
//fill column A with 0.00012345, 6.789, 20.2
range ra=col(A); //range variable
type $(ra, *3); //ANS: 1.23E-4 6.79, 20.2
Display ra with 3 significant digits
math expression .0,
xx = 1234.5678;
type $(xx*4, .0,); //ANS: 4,938
Display expression result with 0 decimal places and comma separator

Format

We use type command to show effect of all formats.

Default Format

The square brackets indicate that format is an optional argument for the $() substitution notation. No no format is specified, Origin will carry expression to the number of decimal digits or significant digits specified by the @SD system variable (which default value is 14). For example:

double aa =  3.14159265358979323846;
type $(aa);    // ANS: 3.1415926535898

Origin Formats

Origin supports custom formatting of numeric values in the worksheet or in text labels. For a full list of numeric format options, see Reference Tables: Origin Formats.


Format Description Example
*n Display n significant digits
xx=1.23456; 
type "xx = $(xx, *2)"; //ANS: xx=1.2
.n Display n decimal places
xx=1.23456; 
type "xx = $(xx, .2)"; //ANS: xx=1.23
*n* Display n significant digits, truncating trailing zeros
xx = 1.10001; 
type "xx = $(xx, *4*)"; //ANS: xx=1.1
.n, Display n decimal places, using comma separator (US, UK, etc.)
xx = 1234.5678;
type "xx = $(xx, .2,)"; //ANS: xx=1,234.57
xx= 10000;
type "$(xx, .0,)"; //ANS: 10,000
E.n Display n decimal places, in engineering format
xx=203465987;
type "xx = $(xx, E*3)"; //ANS: xx=203M
S*n Display n significant digits in scientific notation of the form 1E3
xx=203465987;
type "xx = $(xx, S*3)"; //ANS: xx=2.03E+08
D<format> Display in custom date format, where <format> is either the index number (counting from 0) of the format, starting from the top of the Column Properties Display list; or a string built using these date and time format specifiers.
type "$(date(7/20/2009), D1)";  // ANS: Monday, July 20, 2009
type "$(date(7/20/2009), Dyyyy'-'MM'-'dd)";  //ANS: 2009-07-20
DT=2459858.6946202; //assign a julian date to DT
type "DT = $(DT, D1)"; // ANS: DT = Thursday, October 6, 2022
type "Now= $(@D, DMM-dd-yyyy h:mm:ss tt)"; // @D: current date&time
T<format> Display in custom time format, where <format> is either the index number (counting from 0) of the format, starting from the top of the Column Properties Display list; or a string built using these time format specifiers.
type "$(time(14:31:04), T4)";   //ANS: 02 PM
type "$(time(14:31:04), Thh'.'mm'.'ss)";  //ANS: 02.31.04
DT=2459858.6946202;
type "DT = $(DT, T0)"; // ANS: DT = 16:40
type "Now = $(@D, THH:mm:ss.##)"; // @D: current date&time
C<format> Display month or day of week in Calendar format. <format> is either M# (month), or D#=day of the week. # can be 0 (3 characters), 1 (full characters) or 2 (1 character)
type "$(12, CM0)";  // ANS: Dec
type "$(3, CD1)";  // ANS: Wednesday
#n or ## Display an integer to n places, zero padding where necessary. Or use n number of #'s to refer to nth places
xx=45;
type "xx=$(xx, #5)"; //ANS: 00045
type "xx=$(xx, ###)"; //AnS: 045
<prefix>##<sep>###<suffix> Display a number by specifying a separator (<sep>) between digits and optionally add prefix(<prefix>) and/or suffix (<suffix>). One # symbol indicates one digit. The last # in this expression always refers to the unit digit. The numbers of # in both first and second parts can be varied.
xx=56000;
type "xx=$(xx, ##+###)"; //ANS: xx=56+000
xx=4000; 
type "xx=$(xx, ##+##M)"; //ANS: xx=40+00M
# #/n Round and display a number as a fraction with specified n as denominator. The numerator and denominator are separated by a forward slash /. The number of digits of numerator is adjusted accordingly.
AA = 0.334;
type "AA = $(AA, # ##/##)"; //ANS: AA = 1/3
type "AA = $(AA, # #/8)"; //ANS: AA = 3/8
D[<space>]M[S][F][n] Display a degree number in the format of Degree° Minute' Second", where 1 degree = 60 minutes, and 1 minute = 60 seconds. Space can be inserted to separate each part. n indicates decimal places for fractions. F displays degree number without symbols and inserting spaces as separator.
DD = 37.34255;
type "DD = $(DD, DMS)"; //ANS: DD = 37°20'33"
type "DD = $(DD, D MS)"; //ANS: DD = 37° 20' 33"
type "DD = $(DD, DMSF)"; //ANS: DD = 37 20 33
type "DD = $(DD, DMF1)"; //ANS: DD = 37 20.6

C-Language Formats

The format portion of the $() notation also supports C-language formatting statements.

Option Un/Signed Output Input Range Example
d, i SIGNED Integer values (of decimal or integer value) -2^31 -- 2^31 -1
double nn = -247.56;
type "Value: $(nn,%d)"; // ANS: -247
f, e, E, g, G SIGNED Decimal, scientific, decimal-or-scientific +/-1e290 -- +/-1e-290
double nn = 1.23456e5;
type "Values: $(nn, %9.4f), $(nn, %9.4E), $(nn, %g)";
// ANS: 123456.0000, 1.2346E+005, 123456
double nn = 1.23456e6;
type "Values: $(nn, %9.4f), $(nn, %9.4E), $(nn, %g)";
// ANS: 123456.0000, 1.2346E+006, 1.23456e+006
o, u, x, X UNSIGNED Octal, Integer, hexadecimal, HEXADECIMAL -2^31 -- 2^32 - 1
double nn = 65551;
type "Values: $(nn, %o), $(nn, %u), $(nn, %X)";
// ANS: 200017, 65551, 1000F

Note: In the last category, negative values will be expressed as two's complement.

Combining Origin and C-language Formats

Origin supports the use of formats E and S along with C-language format specifiers. For example:

xx = 1e6;
type "xx = $(xx, E%4.2f)";  // ANS: 1.00M

Display Negative Values

Most of Origin's LabTalk command statements take options and options are always preceded by the dash "-" character. Therefore the following command will not work

k=-5;
type $(k); //intepret as type -5 and think -5 is an option of ''type'' command

Therefore you must protect the - by enclosing the substitution in quotes or parentheses. For example:

K = -5;
type "$(K)"; // This works
type ($(K)); // This works

Variable Naming with $()

In assignment statements, the $() notation is substitution-processed and resolved to a value regardless of which side of the assignment operator it is located.

//create variable A with value 2
A = 2; //Define variable A with value 2.  

//Create a variable A2 with the value 3
A$(A) = 3; $(A) is resolved first so LHS is A2

//verify the result
A2=;
A$(A)=;


Also when variable names are used for range or dataset type argument in functions, $() substitution is needed to resolve it first.

//fill column 1 to 5 with values
col(1)=data(1,100); 
int nn = 5; 
loop(ii,2,nn)
{
  wcol(ii)=normal(100);
}

//Example of using variable in X Function smooth
loop(ii,2,nn)
{
 //smooth on the iith column $(ii)
  smooth $(ii) method:=aav npts:=10;
}

//Example of using variable in LabTalk function sum()
lastcolumn=wks.ncols;
//add a column after last one and set the value to be sum of last column
col($(lastcolumn+1))= sum($(lastcolumn));
col($(lastcolumn+1))[L]$= sum; //set long name of column


--- For more examples of $() substitution, see Numeric to String conversion.