SPLat Logo

Floating point formatting function f()

NOTICE: Our web site is being updated, but is currently experiencing extreme slowness due to host issues. Please contact us directly.
NOTICE: SPLat Controls has moved. We are now at 1/85 Brunel Rd, Seaford, 3198. map
SPLat will be shut down for the Christmas Holiday season from Friday 20 December 2024 through Tuesday 7 January 2025. We will check emails periodically and will continue to process orders but at a slower pace. Santa hat

Floating point formatting function f()

Specifies floating point data as an argument to a supporting hash function.

Formatter Prototype

f(
Source,
FieldWidth,
Decimals,
Style )

 

ParameterOptionDescription
Source mandatory Quite simply, this is what to print
FieldWidth optional Number of characters to print, including sign and decimal point.  Leading spaces will be printed to ensure the number of displayed characters is always this width, unless the Style is set to "v".  Range is 1 thru 9, default is 9.
Decimals optional

Maximum number of decimal places to print.  Range is 0 thru 7, default is 7.  This value cannot be specified without the FieldWidth value.

Style

optional

"v" means "variable width".  If the value is less than the specified width, then no leading spaces will be printed.  Default is fixed width.

The first argument specifies the data source. An optional pair of arguments specifies the format in terms of the number of characters total (FieldWidth) and the number of digits after the decimal point (Decimals). Unless both format arguments are present, the format defaults to FieldWidth=9 and Decimals=7. If the result requires fewer than FieldWidth character it will be padded to the left with spaces unless Style is set to "v".

Source specifiers

The following are the allowable source specifiers

Form Meaning Example

=w

Contents of the W register

#HMI Print(f(=w))
#HMI Print(f(=w,3,1) ;"1.2"
#HMI Print(f(=w,6,3) ;" 1.201"

=q

Contents of the Q register

#HMI Print(f(=q))

Constant

Contents of an EQUated constant. This can be plain EQU, fEQU or #EQU

Foo fEQU 56.78

...

#HMI Print(f(Foo))

Any valid floating point number An immediate numeric value

#HMI Print(f(3.1415926))

#HMI Print("3.1415926")

*Label

Contents of a float in RAM

fTemperature defFLOAT

...

#HMI Print(f(*Temperature))

index/jndex modifiers

RAM variable references can also have index/jndex modifiers

Form What is sourced Example code

*+ff

Whatever is in RAM (float) variable ff, with IasJ: applied

#HMI Print(f(*+fCounter)) ;Force indexing

*-ff

Whatever is in RAM (float) variable ff, with NoJ: applied

#HMI Print(f(*-fCounter)) ;Inhibit jndexing

NOTE: The modifier (+ or -) must be after the asterisk. jndexing applies only to code running inside a MultiTrack task.

Format specifiers

The format arguments FieldWidth and Decimals specify how the number is to be displayed. The results are consistent with the older character LCD OBLCD_fDispW instruction.

Examples (spaces shown as ^):

Example code Result (the single quotes are NOT printed) Notes

#HMI Print(f(1.23456))

' 1.23456'

Defaults to 9, 9

#HMI Print(f(1.23456, 9))

' 1.23456'

Defaults to 9, 9 because FieldWidth and Decimals must be both present to affect the outcome (all or nothing)

#HMI Print(f(1.23456, 5, 3))

'1.235'

Note rounding

#HMI Print(f(-1.23456, 5, 3))

'-1.23'

Limited by f character positions

#HMI Print(f(-1.23456, 5, 9))

' -1.235'

Limited by 3 decimal places; padded left with spaces

#HMI Print(f(12.3456, 5, 3))

'12.35'

 

#HMI Print(f(123.456, 5, 3))

'123.4'

 

#HMI Print(f(1234.56, 5, 3))

' 1235'

Decimal point suppressed

#HMI Print(f(12345.6, 5, 3))

'12346'

 

#HMI Print(f(123456, 5, 3))

'+++++'

Overflow: result can't fit in field

#HMI Print(f(-12345.6, 5, 3))

'-----'

Underflow (negative overflow)

#HMI Print(f(0.123456, 5, 3))

'0.123'

Leading "0." is displayed

#HMI Print(f(-0.123456, 5, 3))

'-0.12'

 

#HMI Print(f(-0.00012345, 5, 3))

'-0.000'

Take care with small numbers!