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 )

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