4.43 format - Formatted Output

Format: format($num$, output format)

mcal internally manages floating point numbers. This function changes the output format of the number by specifying the output format available in the C language printf function. The 3 available formats are as follows.

For example, the decimal format is expressed as 0.00726 can be expressed as 7.260000e-03 in exponent format, similarly, the exponent format of 1265 is represented as 1.265000e+03.

By specifying the % or plus sign after the number, the function formats the decimal digits and characters. The number format can be specified in the format of “whole number.decimal”. For example, the number 123.456789 with the format specifier of %5.2f becomes 123.46, and the format specifier of %8.3f print number as 123.458 with a floating point of 8 characters and 3 characters after decimal.

Add a plus sign before the number to display the plus sign before the number. For example, 123.456789 with the format specifier of %+5.2f format the number as +123.46.

Besides the format highlighted above, it is possible to include any character string in the format. For example, the number 250 can be formatted as “Total 250 Yen” using the format specifier of “Total

Examples

Example 1: Basic Example

Format val as real number with 3 decimal places.

$ more dat1.csv
id,val
1,0.00726
2,123.456789
3,
4,-0.335
$ mcal c='format(${val},"%8.3f")' a=rsl i=dat1.csv o=rsl1.csv
#END# kgcal a=rsl c=format(${val},"%8.3f") i=dat1.csv o=rsl1.csv
$ more rsl1.csv
id,val,rsl
1,0.00726,   0.007
2,123.456789, 123.457
3,,
4,-0.335,  -0.335

Example 2: Display the exponent

Format val in exponential notation.

$ mcal c='format(${val},"%e")' a=rsl i=dat1.csv o=rsl2.csv
#END# kgcal a=rsl c=format(${val},"%e") i=dat1.csv o=rsl2.csv
$ more rsl2.csv
id,val,rsl
1,0.00726,7.260000e-03
2,123.456789,1.234568e+02
3,,
4,-0.335,-3.350000e-01