Click or drag to resize

I/O Format Specifiers for Writing Floating-Point Numbers

Floating-Point Data -- %f (and variants)

A floating point argument has a format specifier of the following form:

%[flags] [488.2 type] [width] [.precision] [delimiter [array size] ] [$S] [l | L] [f | e | E | g | G]

Modifier

Interpretation

Default Functionality

The argument is formatted as a floating point number in NR2 format (a number with at least one digit after the decimal point).

flags

+, -, 0

Controls justification and padding of the output, as follows:

  • - - left aligns the result with the given field width.

  • + - prefixes the output value with a sign (+ or –) if the output value is of a signed type. By default, a sign only appears for negative values.

  • 0 pads with zeros until the specified width is reached. If 0 and - appear together, the 0 is ignored.

488.2 type

The number is formatted in one of six standard IEEE 488.2 numeric formats. The supported formats are:

  • @1 - IEEE 488.2 NR1 format (integer without any decimal point). Example: 123

  • @2 - IEEE 488.2 NR2 format (number with at least one digit after the decimal point). Example: 123.45

  • @3 - IEEE 488.2 NR3 format (floating-point number in exponential form). Example: 1.2345E-67

  • @H - IEEE 488.2 hex format. Example: #HAF35B

  • @Q - IEEE 488.2 octal format. Example: #Q71234

  • @B - IEEE 488.2 binary format. Example: #B011101001

width

Minimum field width of the output number.

An asterisk (*) may be present in lieu of an integer width modifier, in which case an extra int argument supplies the value.

precision

Maximum number of digits after the decimal point for %f, %e, %E.

Maximum number of significant digits for %g, %G.

An asterisk (*) may be present in lieu of an integer precision modifier, in which case an extra int argument supplies the value.

delimiter

[array size]

The argument is interpreted as an array of floats (or double or long double, as specified by the length). If the $S modifier is present, the argument is interpreted as a SAFEARRAY of floating-point numbers.

array size specifies the number of array elements to send.

An asterisk (*) may be present in lieu of an integer array size modifier, in which case an extra int argument supplies the value.

$S

The argument is interpreted as a SAFEARRAY of floating-point numbers.

length modifier

  • l - argument is a double.

  • L - argument is a long double.

type

  • f - signed value having the form dddd.dddd

    where dddd is one or more digits.

    The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision.

    Example: 3.1415

  • e - signed value having the form d.dddde [+ | -]ddd

    where d is a single digit, dddd is one or more digits, ddd is exactly three digits.

    Example: 3.14e-123

  • E - identical to e format except that E rather than e introduces the exponent.

    Example: 3.14E-123

  • g - signed value in f or e format, whichever is more compact for the given value and precision.

    The e format is used only when the exponent of the value is less than –4 or greater than or equal to the precision.

    Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it.

  • G - Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate)..

Example -- Width and precision

C++
HRESULT hr = S_OK;

double dRange = 300;

// Default precision is 6, "l" modifier promotes to a double
hr = io.Printf(_T("%lf"), dRange);                        // Sends "300.000000"

// Specify width of 4, but required length is greater
hr = io.Printf(_T("%4.2lf"), dRange);                     // Sends "300.00"

// Width requires padding
hr = io.Printf(_T("%8.2lf"), dRange);                     // Sends "  300.00"

// Exponential notation
hr = io.Printf(_T("%8.2le"), dRange);                     // Sends "3.00e+02"

// Supply field width using a variable
int nWidth = 8;
hr = io.Printf(_T("%*.2le"), nWidth, dRange);             // Sends "3.00e+02"

// Supply field width and precision using variables
int nPrecision = 2;
hr = io.Printf(_T("%*.*le"), nWidth, nPrecision, dRange); // Sends "3.00e+02"

Example -- Floating-point arrays

C++
HRESULT hr = S_OK;

double rgData[] = { 1.1, 1.2, 1.3, 1.4, 1.5 };

// Comma-separated list of 3 elements
hr = io.Printf(_T("%4.2,3lf"), rgData);               // Sends "1.10,1.20,1.30"

// Semi-colon separated, count supplied as a variable
int nCount = 4;
hr = io.Printf(_T("%3.2(;)*le"), nCount, rgData);     // Sends "1.10e+00;1.20e+00;1.30e+00;1.40e+00"

CComSafeArray<double> saData;
saData.Add(1.1);
saData.Add(1.2);
saData.Add(1.3);

// Comma-separated list.  Argument is a SAFEARRAY.
// No field width supplied, so count is determined from the SAFEARRAY itself.
hr = io.Printf(_T("%4.2,$Slf"), rgData);              // Sends "1.10,1.20,1.30"
See Also

Other Resources

Download a complete CHM version of this documentation here.