Keysight VISA.NET Help
Formatting Floating-Point Numbers

Formatting Floating-Point Numbers

A floating point argument has a format specifier in one of the following forms:

%f%[-| |+|@1|@2|@3|@H|@Q|@B][[0]width][.precision][,[arraySize]]f

%e%[-| |+|@1|@2|@3|@H|@Q|@B][[0]width][.precision][,[arraySize]]e

%E%[-| |+|@1|@2|@3|@H|@Q|@B][[0]width][.precision][,[arraySize]]E

%g%[-| |+|@1|@2|@3|@H|@Q|@B][[0]width][.precision][,[arraySize]]g

%G%[-| |+|@1|@2|@3|@H|@Q|@B][[0]width][.precision][,[arraySize]]G

Note that they are identical except for the format type.

Modifier

Interpretation

Default Functionality

The argument is interpreted as a floating-point number.

flags

-, +, space, 0

@1, @2, @3

@H, @Q, @B

Controls justification and padding of the output, as follows:

  • '-' left aligns the result with the given field width.
  • '+' prefixes the value with the sign (- or +).   Ignored with @H, @Q, or @B.
  • ' ' prefixes the value with the sign (- or space).   Ignored with @H, @Q, or @B.
  • '0' pads the value with '0'.   Ignored with '-', @H, @Q, or @B.
  • '@1' formats the value in IEEE 488.2 NR1 format.
  • '@2' formats the value in IEEE 488.2 NR2 format.
  • '@3' formats the value in IEEE 488.2 NR3 format.
  • '@H' formats the value in IEEE 488.2 hexadecimal format.
  • '@Q' formats the value in IEEE 488.2 octal format.
  • '@B' formats the value in IEEE 488.2 binary format.

width

For types e, E and f, the number of digits after the decimal point.

For types g and G, the number of significant digits.

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

.precision

Maximum number of characters to send.

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

[,[arraySize]]

Formats an array of integers converted to strings.  If arraySize is specified, it is the maximum number of elements to include in the formatted array.

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

Floating Point Formatting Code Snippets

Remember that Printf is adding characters to the formatted write buffer. The comments after each Printf call show what character(s) are added to the formatted write buffer by that call. Assume that the io variable is a valid reference to IMessageBasedFormattedIO.

No Modifiers
Copy Code
io.Printf("|%f|", 15.5);              // Adds |15.500000|
io.Printf("|%f|", -1.50e+020);        // Adds |-150000000000000000000.000000|
io.Printf("|%e|", 15.5);              // Adds |1.550000e+001|
io.Printf("|%E|", 15.5);              // Adds |1.550000E+001|
io.Printf("|%g|", 15.5);              // Adds |15.5|
io.Printf("|%G|", 15.5);              // Adds |15.5|
io.Printf("|%g|", 1.50E+020);         // Adds |1.5e+20|
io.Printf("|%G|", -1.50e+020);        // Adds |-1.5E+20|
io.Printf("|%f|\n", -15.5);           // Adds |-15.500000|, sends buffer & flushes
Sign Modifiers
Copy Code
io.Printf("|% f|", 15.0);             // Adds | 15.000000|
io.Printf("|% e|", -15.0);            // Adds |-1.50e+001|
io.Printf("|%+G|", 15.5);             // Adds |+15.5|
Width and Precision Modifiers
Copy Code
io.Printf("|%12f|", 15.0);            // Adds |   15.000000|
io.Printf("|%4f|", 15.0);             // Adds |15.000000|
io.Printf("|%-*f|", 12, 15.0);        // Adds |15.000000   |
io.Printf("|%.2f|", 15.0);            // Adds |15.00|
io.Printf("|%.*f|", 4, 15.0);         // Adds |15.0000|
io.Printf("|%+*.*f|", 12, 2, 15.0);   // Adds |      +15.00|
Floating-Point Arrays
Copy Code
double[] data = new double[] { 1.1, 1.2, 1.3};

io.Printf("|%4.2,f|", data);          // Adds |1.10,1.20,1.30|
io.Printf("|%4.2,2f|", data);         // Adds |1.10,1.20|
Io.Printf("|%3.2,*e|", 2, data);      // Adds |1.10e+000,1.20e+000|
PrintfArray
Copy Code
unsafe
{
    double[] data = new double[] { 1.1, 1.2, 1.3};
    fixed (double* pdata = data)
    {
        io.PrintfArray("|%3.2,2e|", pdata);   // Adds |1.100e+00,1.200e+00|
    }
}

 

 

 

 


© Keysight Technologies 2015-2025