0% found this document useful (0 votes)
39 views3 pages

Format Specifiers in LabVIEW Explained

The document outlines the syntax and usage of format specifiers in string formatting functions, detailing optional elements and their meanings. It describes how to format numbers, strings, and time, including various conversion codes and localization codes. Additionally, it provides examples of format specifiers to illustrate their application in different contexts.

Uploaded by

jegodman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views3 pages

Format Specifiers in LabVIEW Explained

The document outlines the syntax and usage of format specifiers in string formatting functions, detailing optional elements and their meanings. It describes how to format numbers, strings, and time, including various conversion codes and localization codes. Additionally, it provides examples of format specifiers to illustrate their application in different contexts.

Uploaded by

jegodman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

In This Topic

Format Specifier Syntax


Format Specifiers Syntax
You use format specifiers to format strings, convert a number into a string, and insert non-displayable Elements
characters in strings.
Format Specifier
For functions that produce a string as an output, such as Format Into String and Array To Spreadsheet String, Examples
a format specifier uses the following syntax elements. Double brackets ( [] ) enclose optional elements.
%[$][-][+][#][^][0][Width][.Precision || _SignificantDigits][{Unit}][<Embedded information>]Conversion Code

where Width must be a number greater than zero and .Precision and _SignificantDigits must be a number greater than or equal to
zero.
For functions that scan a string, such as Scan From String and Spreadsheet String To Array, a format specifier uses the following simplified
syntax elements.
%[Width]Conversion Code

The Format Into String, Format Into File, Scan From String, and Scan From File functions can use multiple format specifiers in the format
string input, one for each input or output of the expandable function.

Format Specifiers Syntax Elements


The following table displays the syntax elements for format specifiers. Refer to the examples of format specifiers for more information.

Syntax Element Description


% Begins the format specifier.
$ (optional) When you use a formatting function, this modifier specifies the order in which to display variables. Include the digit that
represents the order of the variable immediately before this modifier.
- (optional) When you use a formatting function, this modifier left justifies the parameter rather than right justifies it within its
width.
+ (optional) When you use a formatting function, this modifier includes sign even when the number is positive.
^ (optional) When you use a formatting function and the e or g conversion codes, this element formats the number in engineering
notation, where the exponent is always a multiple of three.
# (optional) When you use a formatting function, this modifier removes trailing zeros. If the number has no fractional part, this
modifier also removes the description part.
0 (optional) When you use a formatting function, use this modifier without the - modifier to pad any excess space to the left of a
numeric parameter with zeros rather than with spaces to reach minimum width.
Width (optional) When you use a scanning function, such as Scan From String, the Width element specifies the maximum character field
width to use. LabVIEW scans the maximum number of characters when processing the parameter. When you use a
formatting function, the Width element specifies the minimum character field width of the output. This width is not a
maximum width. LabVIEW uses as many characters as necessary to format the parameter without truncating it.
LabVIEW pads the field to the left or right of the parameter with spaces, depending on justification. If Width is missing
or 0, the output is only as long as necessary to contain the converted input parameter.
.Precision or When you use a formatting function, . or _ controls the number of digits displayed. If you use ., LabVIEW uses the
_Significant number that follows as a precision specifier for digits to the right of the decimal point. If you use _, LabVIEW uses the
Digits (optional) number that follows as the specified number of significant digits to use in the display format.
.Precision—When you use it with floating-point notation, this element specifies the number of digits to the right of the
decimal point. If . is not present, LabVIEW uses a precision of six digits. If . is 0, LabVIEW does not insert a precision.
When you use it with string parameters, .Precision specifies the maximum width of the scanned field. LabVIEW
truncates strings longer than this length.
_Significant Digits—Displays the data by rounding to the number of digits you specify. LabVIEW rounds the data
only for display purposes, which does not affect the original data. .Precision affects only the digits to the right of the
decimal point, and _Significant Digits includes all non-spacing digits. For example,
l 3.457 has 4 significant digits
l 0.0012 has 2 significant digits
l 123000 has 3 significant digits

Note You cannot use precision and significant digits together in a single format specifier.

For single-precision, floating-point numbers, National Instruments recommends that you use values from 1 to 6 for
_Significant Digits. For double-precision and extended-precision, floating-point numbers, National Instruments
recommends that you use values from 1 through 13 for _Significant Digits.
{Unit} (optional) Overrides the original unit of a VI when you use a function to convert a physical quantity (a value with an associated
unit). You must use a compatible unit. You can use this syntax element only with the Format Into String function.
<Embedded Time Contains a time-specific format string for use with the T (absolute time) and t (relative time) conversion codes. Refer
Format> (optional) to the Format Codes for the Time Format String for valid format strings. Only %W, %D, %H, %M, %S and %u apply to relative
time.

Page 1 of 3
Conversion Codes Characters that specify how to scan or format a parameter.
Use the following conversion codes for integers and fixed-point numbers:
l x—Hexadecimal integer (for example, B8).
l o—Octal integer (for example, 701).
l b—Binary integer (for example, 1011).
l d—Signed decimal integer.
l u—Unsigned decimal integer.

Use the following conversion codes for floating-point and fixed-point numbers:
l f—Floating-point number with fractional format (for example, 12.345).
l e—Floating-point number in scientific notation (for example, 1.234E1).
l g—LabVIEW uses f or e depending on the exponent of the number. LabVIEW uses f if the exponent is greater
than –4 or less than the precision specified. LabVIEW uses e if the exponent is less than –4 or greater than the
precision specified.
l p—Floating-point number in SI notation.

Use the following conversion codes for a string:


l s—String (for example, abc). When scanning, s matches only up to the next white-space character. A space
matches one or more consecutive white-space characters. To scan a string that may contain white space, use the
characters in set conversion code. Specify all the characters which the string may contain within the brackets,
including space or other white-space characters.
l [ ]—Characters in set. [ ] matches a string that contains only the characters specified between the brackets.
Character matches are case sensitive. The [ ] conversion code is useful only when you are scanning strings. To
match the caret symbol (^) in a set, make sure it is not the first character after the bracket. The following
examples demonstrate the use of the characters in set conversion code:

%[aeiou]—Matches any string that contains only lowercase vowels.

%[0-9a-zA-Z ]—Matches a string that contains numbers, letters, or spaces. You can use a hyphen to specify
ranges of characters in the set.

%[^,;]—Matches any string of characters up to but not including the first comma or semicolon.
To match a hyphen, specify it as the first or last character in the set.
Use the following conversion codes for time:
l T—Absolute time. Use T in the Display Format property of numeric or time stamp controls, constants, and
indicators, as well as the Format Into String, Format Into File, Scan From String, and Scan From File functions.
Refer to the Format Specifiers Examples table for examples of using absolute time. You cannot use T with the
Array To Spreadsheet String, Spreadsheet String To Array, Format Value, and Scan Value functions. Formatting a
time stamp as anything other than time returns an error.
l t—Relative time. Use t in the Display Format property of numeric or time stamp controls, constants, and
indicators, as well as the Format Into String, Format Into File, Scan From String, and Scan From File functions.
Refer to the Format Specifiers Examples table for examples of using relative time. You cannot use t with the Array
To Spreadsheet String, Spreadsheet String To Array, Format Value, and Scan Value functions. Formatting a time
stamp as anything other than time returns an error.
Localization Codes Characters that determine if LabVIEW uses a decimal or comma to separate the whole number from the decimal part of
the number. These codes control the decimal separator for numeric output. These codes do not cause any input or
output to occur. They change the decimal separator for all further inputs and outputs until they find the next %;.
%,; Comma decimal separator.
%.; Period decimal separator.
%; System default separator. If you do not specify a separator, LabVIEW uses the system default separator.
Backslash (\) Characters that specify hex values, spacing, backspaces, and other formatting options.
Codes
Top
Format Specifier Examples
The following table displays examples using the syntax elements for format specifiers.

Format
Type Argument(s) Resulting String Comments
String
Automatic 12.00 %#g 12 If you specify #, LabVIEW removes trailing zeros. If you specify g,
Formatting (% LabVIEW chooses scientific notation or floating-point notation based
g) 12000000 %#g 1.2E+7 on the number to format.
Decimal (%d) 12.67 score= %d%% score= 13% When you specify %d, LabVIEW displays integers in base-10 decimal
format.
l For signed or unsigned integers of any bit size, LabVIEW
displays the integer unmodified.

Page 2 of 3
l LabVIEW rounds floating-point values to signed 64-bit
integers. NaN and values greater than the maximum value of a
signed 64-bit integer are rounded to the maximum value of a
signed 64-bit integer, which is 9223372036854775807. To
record values greater than the maximum value of a signed 64-
bit integer, you must explicitly cast the values to unsigned 64-
bit integers using the To Unsigned Quad Integer function.
Floating-Point 12.67 Temp: %5.1f Temp: 12.7 The 5 in the Format String section specifies a width of 5, and the
(%f) 1 specifies the number of digits to the right of the decimal, or
precision.
12.67 N %5.3f 12.670 N Units are valid only if you use the Format Into String function.
These are examples of physical quantity input. The second example
12.67 N %5.3{mN}f 12670.000 mN shows how you can convert from one unit to another. The question
mark indicates when the unit in the format specifier is in conflict
12.67 N %5.3{kg}f 12.670 ?kg
with the input unit.
Scientific 12.67 %.3e 1.267E+1 Add ^ to change to engineering notation where the exponent is
Notation (%e) 12.67 %^.3e 12.670E+0 always a multiple of three.

SI Notation 12000000 %.2p 12.00M A value of .2 specifies that you want a precision of 2 digits in the
(%p) resulting string. A value of _2 specifies that you want two significant
12000000 %_2p 12M digits in the resulting string. %p adds the SI prefixes.
Hexadecimal 12 %02x 0C A – indicates that you want the resulting string left justified. A 0
(%x) indicates that you want the resulting string padded with zeros. The
number specifies the width of the resulting string. In other words, a
Octal (%o) 12 %06o 000014
value of –n pads the resulting string with spaces to the n width. b is
Binary (%b) 12 %b 1100 base 2, o is base 8, and x is base 16.

Relative Time 91.80 %.2t 01:31.80 LabVIEW formats elapsed time in terms of complete weeks (%W),
(%t) 91.8 %<Hours:%H Hours:00 days (%D), hours (%H), minutes (%M), seconds (%S), and fractions of
Minutes:%M Minutes:01 seconds (%u). Refer to the Format Codes for the Time Format String
Seconds:% Seconds:31 for more time format codes.
S>t
Absolute [Link].000 AM %<%.3X %x>T [Link].000 AM %T specifies absolute time. Any information you include with the <
Time (%T) 1/1/2001 01/01/2001 and > brackets indicates how you want to format the absolute time.
(Universal Time) This format, including the punctuation, changes based on the
[Link].000 AM %<%Y.%m.% 2001.01.01 regional settings of the computer. The time changes based on the
1/1/2001 d>T configured time zone for the computer. Refer to the Format Codes
(Universal Time) for the Time Format String for more time format codes.
[Link].000 AM %^<%.3X % [Link].000 AM Add ^ to format the time in Universal Time.
1/1/2001 x>T 12/31/2000
(Universal Time)
String (%s) Smith John Name: %s, % Name: Smith, Strings are valid only if you use the Format Into String function. In
s. John. the second example, the format string specifies to use at most six
Hello, World String: % String: Hello, characters from the string Hello, World then pad with spaces so
10.6s the total string length is equal to 10.

Variable Smith John %2$s %1$s John Smith In this example, Smith is the first variable and John is the second
Order (%$) variable. If you use the $ syntax element in the format string for a
particular variable, subsequent syntax elements that are not
preceded by a $ syntax element apply to the next variable. If you
use the $ syntax element for a variable, use the $ syntax element
for any other variables in the string to improve readability.
Percent None %% % %% formats a single %. This format provides a way to include an
Character (% actual percent character in the string. It does not add a terminal to
%) the Format Into String or Scan From String functions.
Top

Page 3 of 3

You might also like