FormatFloat (Convert Floating Point to String)
FormatFloat converts a floating-point number (Float) into a String using a printf-style format pattern (similar to C’s printf). Use it when you need a number to appear in text output (serial messages, display strings, custom log lines, etc.).
If you need to format multiple values (time + several numbers, mixed types) into one string, consider using Sprintf instead.
Syntax
String = FormatFloat ( Float, FormatString )
Example1:
The following program shows the use of the FormatFloat instruction. Using the software's numeric monitor, enter a value into FloatVal that should be converted into a string value.
Public FloatVal as Float
Public StringVal as String * 20
BeginProg
Scan (1,Sec,3,0)
StringVal = FormatFloat (FloatVal,"%f")
NextScan
EndProg
Example 2:
The following program example is useful when a display expects a clean numeric string with fixed decimal places.
Public AirTemp_C
Dim OutString As String * 20
BeginProg
Scan(1,Sec,0,0)
SerialOpen (ComC1,-9600,0,0,50)
' Example measurement
AirTemp_C = 23.4567
' Format to 2 decimal places
OutString = FormatFloat("%.2f", AirTemp_C)
' Send to serial display with carriage return/line feed
SerialOut(COMC1, OutString & CHR(13) & CHR(10), "", 0, 0)
NextScan
EndProg
Example 3:
The following is an example of formatting output so it is easily read by a person.
Public BattV
Dim OutString As String * 40
BeginProg
SerialOpen (ComC1,-9600,0,0,50)
Scan(10,Sec,0,0)
' Example measurement
BattV = 12.9876
' Build a labeled message
OutString = "Battery Voltage: " & FormatFloat("%.2f", BattV) & " V"
' Send message
SerialOut(ComC1, OutString & CHR(13) & CHR(10), "", 0, 0)
NextScan
EndProg
Example 4:
This example is useful when another device or file expects comma-separated values.
Public AirTemp_C, RH, BattV
Dim OutString As String * 80
BeginProg
SerialOpen (ComC1,-9600,0,0,50)
Scan(10,Sec,0,0)
'Example measurements
AirTemp_C = 22.5
RH = 56.234
BattV = 12.987
'Create CSV text line
OutString = FormatFloat("%.1f", AirTemp_C) & "," & _
FormatFloat("%.1f", RH) & "," & _
FormatFloat("%.2f", BattV)
'Send CSV line
SerialOut(ComC1, OutString & CHR(13) & CHR(10), "", 0, 0)
NextScan
EndProg
Remarks
The string conversion of the floating point value is formatted based on the FormatString parameter.
Parameters
Float (Floating Point)
The variable or constant that holds the floating point value to be converted.
Type: Variable or Constant
FormatString
Determines how the floating point value will be represented in the converted string.
Most formats follow this template: %[width].[precision]type
Where:
-
% means: “a formatted value goes here.” Everything after % up to the final type letter describes how to format width and precision.
-
width (optional) is the minimum number of characters to use (pads with spaces unless zero-pad is used)
TIP:If precision is not specified the default is 6 decimal places, regardless of the width parameter.
-
precision (optional) = number of digits after the decimal (for f)
-
type = formatting style: f, e/E, or g/G
Supported Type Codes
-
%f — Fixed decimal notation
-
%e / %E — Scientific notation
-
%g / %G — “General” (use the shortest representation; chooses f or e)
| Example formats for a floating point value | |||
|---|---|---|---|
| Format String | Original value | Formatted value | Format description |
| "%f" | 22.5 | 22.500000 | Default 6 decimals |
| "%.2f" | 22.5 | 22.50 | 2f specifies 2 decimals. No specified total width |
| "%8.2f" | 22.5 | 22.50 | Total width = 8; decimals = 2. Notice 3 leading spaces |
| "%08.2f" | 22.5 | 00022.50 |
Total width = 8; decimals = 2. Zero=padded so 0s replace spaces |
| "%e" | 22.5 | 2.250000e+01 |
Scientific notation; Width = 8 |
| "%g" | 22.5 | 22.5 | General; most compact |
NOTE: Y and Z in the table below are only place holders. Replace the them with numbers.
| Type Code | Description (m = mantissa; d = decimal; x = exponent) |
|---|---|
| %f | Decimal notation in the form of ±mmm.dddddd; precision is 6 places to the right of the decimal. |
| %e (or %E) | Decimal notation in the form of ±m.dddddd e±xx; precision is 6 places to the right of the decimal. |
| %g (or %G) | Mantissa and decimal are variable; trailing 0s and decimals are omitted if the input has a precision less than specified by the format string. |
| %Y.Zf | Decimal notation in the form of ±m.d; field width is defined by Y and includes the sign and decimal place. Precision is defined by Z. |
| %0Y.0Zf | Decimal notation in the form of ±m.d; field width is defined by Y and includes the sign and decimal place. The mantissa will be padded by leading 0s if necessary. Precision is defined by Z. The decimal will be padded with trailing 0s. |
| %Ye (or %YE) | Decimal notation in the form of ±m.d e±xx; field width is defined by Y and includes the sign and decimal place. |
| %Yg (or %YG) | Mantissa and decimal are variable; field width is defined by Y and includes the sign and decimal place. |
Right-click the parameter to display a list of options. The format string must be enclosed in quotes.
Including Literal Text
Other ASCII characters may be included in the format string. The %... portion inserts the formatted number; everything else inside the quotes is literal text.
Example:
StringVal = FormatFloat(FloatVal, "Temp=%.1f C")
Returns Temp= 23.5 C