Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2024.1.0 - Русский


printf_conversion

mprintf, msprintf, mfprintf C-format specifications

Description

Each conversion specification in the mprintf , msprintf , mfprintf format string has the following syntax:

  • A % (percent) sign.

  • An optional integer n ≥ 1 followed by "$". n is the index of the input data to substitute to the placeholder, in the msprintf, mprintf .. list of input data. In a format string, placeholders are either all numbered or all non-numbered. A given numbered placeholder can be used only once in its C-format string (Scilab limitation).

  • Zero or more options, which modify the meaning of the conversion specification. The following list contains the option characters and their meanings:

    - : Left align, within the field, the result of the conversion.
    + : Begin the result of a signed conversion with a sign (+ or -).
    ' ' : (space) Prefix a space character to the result if the first character of a signed conversion is not a sign. If both the (space) and + options appear, the (space) option is ignored.
    # : Convert the value to an alternate form. For c, d, i, s, and u conversions, the # option has no effect. For o conversion, # increases the precision to force the first digit of the result to be a 0 (zero). For x and X conversions, a nonzero result has 0x or 0X prefixed to it. For e, E, f, g, and G conversions, the result always contains a decimal point, even if no digits follow it. For g and G conversions, trailing zeros are not removed from the result.
    0 : Pad to the field width, using leading zeros (following any indication of sign or base) for d, i, o, u, x, X, e, E, f, g, and G conversions; no space padding is performed. If the 0 and \- (dash) flags both appear, the 0 flag is ignored. For d, i, o u, x, and X conversions, if a precision is specified, the 0 flag is also ignored.
  • An optional decimal digit string that specifies the minimum field width. If the converted value has fewer characters than the field width, the field is padded on the left to the length specified by the field width. If the left-adjustment option is specified, the field is padded on the right.

  • An optional precision. The precision is given by a dot. followed by a decimal digit string. If no precision is given, the parameter is treated as 0 (zero). The precision specifies:

    • The minimum number of digits to appear for d, u, o, x, or X conversions.
    • The number of digits to appear after the decimal point for e, E, and f conversions.
    • The maximum number of significant digits for g and G conversions.
    • The maximum number of characters to be printed from a string in an s conversion.
  • A character that indicates the type of conversion to be applied:

    % : Performs no conversion. Prints %.
    This may be useful for instance to print percentages, or to process some LaTeX expression including LaTeX comments starting with "%", etc.
    s : Accepts a string or boolean value and displays characters from the string to the end or the number of characters indicated by the precision is reached. If no precision is specified, all characters up to the end are displayed. UTF-8 extended characters are supported in input strings. Booleans are converted into 'T' or 'F'.
    c : Not supported.
    All following conversions accept any decimal numerical or boolean input value. Only the real part of any input complex number is considered. Booleans are implicitly converted into 0 and 1.
    d,i : Converts the input value to a signed integer int32 notation. Conversions for input |Numbers| ≥ 2^31 are not reliable. The precision specifies the minimum number of digits to appear. If the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is a null string. Specifying a field width with a zero as a leading character causes the field width value to be padded with leading zeros.
    u : Converts the input value to an unsigned integer uint32 notation. Conversions for input |Numbers| ≥ 2^32 are not reliable. The precision specifies the minimum number of digits to appear. If the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is a null string. Specifying a field width with a zero as the leading character causes the field width value to be padded with leading zeros.
    o : Converts the input value to an unsigned octal notation. Conversions for input |Numbers| ≥ 2^32 are not reliable. The precision specifies the minimum number of digits to appear. If the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is a null string. Specifying a field width with a zero as the leading character causes the field width value to be padded with leading zeros. An octal value for field width is not implied.
    x, X : Converts the input value to an unsigned hexadecimal notation. Conversions for input |Numbers| ≥ 2^32 are not reliable. The letters ``abcdef'' are used for the x conversion; the letters ``ABCDEF'' are used for the X conversion. The precision specifies the minimum number of digits to appear. If the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is a null string. Specifying a field width with a zero as the leading character causes the field width value to be padded with leading zeros.
    f : Converts the input value to a decimal notation in the format %[\-]ddd.ddd. The number of digits after the decimal point is equal to the precision specification.
    • If no precision is specified, six digits are output.
    • If the precision is zero, no decimal point appears and the system outputs a number rounded to the integer nearest to value.
    • If a decimal point is output, at least one digit is output before it.
    e, E : Converts the input value to the exponential form %[\-]d.ddde+/\-dd. There is one digit before the decimal point, and the number of digits after the decimal point is equal to the precision specification.
    • If no precision is specified, six digits are output.
    • If the precision is zero, no decimal point appears.
    • The E conversion character produces a number with E instead of e before the exponent. The exponent always contains at least two digits. If the value is zero, the exponent is zero.
    g, G : Converts the input value in the style of the e, E, or f conversion characters, with the precision specifying the number of significant digits. Trailing zeros are removed from the result. A decimal point appears only if it is followed by a digit. The style used depends on the value converted. Style e (E, if G is the flag used) results only if the exponent resulting from the conversion is less than -4, or if it is greater or equal to the precision.

A field width or precision can be indicated by an * (asterisk) instead of a digit string. In this case, an integer value parameter supplies the field width or precision. The value parameter converted for output is not fetched until the conversion letter is reached, so the parameters specifying field width or precision must appear before the value to be converted (if any).

If the result of a conversion is wider than the field width, the field is expanded to contain the converted result.

The representation of the plus sign depends on whether the + or (space) formatting option is specified.

The display of exponential form %e is platform dependent with a different number of digits in exponent.

Platform Example: msprintf("%e",1.23e4)
Windows 1.23000e+004
Linux/Mac OS 1.23000e+04

Special escaped sequences are supported in Scilab C-format strings:

\n : Go to Next line (line feed)
\r : Return: go to the head of current line (for overprinting)
\t : horizontal Tab
\\ : print a backslash \

Examples

mprintf('a string: %s\n', 'Scilab');
mprintf('an integer: %d\n', 10);
mprintf('an integer: %4d\n', 10);
mprintf('a left justified integer: %-4d\n', 10);
mprintf('an integer converted to float: %#fd\n',10);
mprintf('an integer with a sign: %+4d\n', 10);
mprintf('an integer with a sign: %+4d\n', -10);
mprintf('an integer padded with zeros: %04d\n', 10);
mprintf('an unsigned integer: %u\n', 10);
mprintf('an unsigned integer: %4u\n', -10);
mprintf('an integer converted to hexadecimal: %x\n', 10);
mprintf('a float: %d\n', %pi);
mprintf('a float: %3.2d\n', %pi);
mprintf('a float (exponential form): %3.2e\n', %pi);
mprintf('a float (exponential form): %3.2g\n', %pi);
mprintf('a character: %c\n', 'a');
mprintf('a character: %c\n', 'aaa');

With input booleans:

mprintf("\n%%d: %d,  %%u: %u,  %%o: %o,  %%f: %f,  %%e: %e,  %%s: %s\n" + ..
          "%%d: %d,  %%u: %u,  %%o: %o,  %%f: %f,  %%e: %e,  %%s: %s\n", ..
        %T, %T, %T, %T, %T, %T, %F, %F, %F, %F, %F, %F);
%d: 1,  %u: 1,  %o: 1,  %f: 1.000000,  %e: 1.000000e+00,  %s: T
%d: 0,  %u: 0,  %o: 0,  %f: 0.000000,  %e: 0.000000e+00,  %s: F

With numbered placeholders:

mprintf("%2$s is %1$d-year old.\n", 32, "Peter");
Peter is 32-year old.

With escaped sequences and UTF-8 extended characters:

mprintf("The path T:\\abc does not exist.\n");
mprintf("abcdefghijk\tαβδ\tεϵ\tζηθικλ\rABCDE\n");
--> mprintf("The path T:\\abc does not exist.\n");
The path T:\abc does not exist

--> mprintf("abcdefghijk\tαβδ\tεϵ\tζηθικλ\rABCDE\n");
ABCDEfghijk αβδ εϵ  ζηθικλ

See also

  • mprintf — converts, formats, and writes data to the main scilab window
  • mfprintf — преобразует, форматирует и записывает данные в файл
  • msprintf — converts, formats, and writes data in a string

History

ВерсияОписание
6.1.0 Numbered placeholders "%n$.." are supported.
6.1.1 Input boolean data can be converted.
Report an issue
<< print Output functions Xcos >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Mon Jun 17 17:55:07 CEST 2024