SetFormat

Sets the format of integers and floating point numbers generated by math operations.

SetFormat, NumberType, Format

 

Parameters

NumberType

Must be either INTEGER or FLOAT.

Format

For NumberType INTEGER, this parameter must be H or HEX for hexadecimal, or D for decimal. Hexadecimal numbers all start with the prefix 0x (e.g. 0xFF).

For NumberType FLOAT, this parameter is TotalWidth.DecimalPlaces (e.g. 0.6).

TotalWidth is typically 0 to indicate that number should not have any blank or zero padding. If a higher value is used, numbers will be padded with spaces or zeroes (see below) to make them that wide.

DecimalPlaces is the number of decimal places to display (rounding will occur). If blank or zero, neither a decimal portion nor a decimal point will be displayed, that is, the number will be stored as an integer rather than a floating point number.

Padding: If TotalWidth is high enough to cause padding, spaces will be added on the left side; that is, each number will be right-justified. To use left-justification instead, precede TotalWidth with a minus sign. To pad with zeroes instead of spaces, precede TotalWidth with a zero.

 

Remarks

If this command is not used in a script, integers default to decimal format, and floating point numbers default to TotalWidth.DecimalPlaces = 0.6. Every newly launched thread (such as a hotkey, custom menu item, or timed subroutine) starts off fresh with the default setting for this command. That default may be changed by using this command in the auto-execute section (top part of the script).

You can determine whether a variable contains a numeric value by using "if var is number/integer/float"

A variable containing a number can be forced into a new format (as set by this command) by adding zero to it, e.g. Var += 0 or Var += 0.0

The built-in variable A_FormatInteger contains the current integer format (H or D). A_FormatFloat contains the current floating point format.

To pad an integer with zeros or spaces without having to use floating point math on it, follow this example:

Var := "          " . Var     ; The quotes contain 10 spaces.  To pad with zeros, substitute zeros for the spaces.
StringRight, Var, Var, 10  ; This pads the number in Var with enough spaces to make its total width 10 characters.

 

Floating Point Format

Since AutoHotkey stores all variables as strings, the stored precision of floating point numbers is determined by DecimalPlaces. In other words, once a variable is rounded off, the extra precision is lost and cannot be reclaimed without redoing the calculation using a higher value of DecimalPlaces.

Any leading or trailing spaces (i.e. padding) in a floating point value will be lost if that value is explicitly assigned to another variable. Use AutoTrim to prevent this.

For the current float format to take effect in a math operation such as addition, at least one of the inputs must contain a decimal point.

A variable containing an integer can be "converted" into a floating point number by adding 0.0 to it, e.g. Var += 0.0. However, if the current float format specifies no DecimalPlaces, use this instead:
if Var is integer
     Var = %Var%.0

 

Hexadecimal Format

Hexadecimal numbers all start with the prefix 0x (e.g. 0xA9). They can be used anywhere a numerical value is expected. For example, Sleep 0xFF is equivalent to Sleep 255 regardless of the current integer format set by SetFormat.

To convert an integer from decimal to hexadecimal, use this example (the converse can be used to convert in the opposite direction):
SetFormat, integer, hex
VariableContainingAnInteger += 0
SetFormat, integer, d

 

Related

Expression assignment (:=), EnvAdd, EnvSub, EnvMult, EnvDiv, AutoTrim, if var is type

 

Example

Var = 11.333333
SetFormat, float, 6.2
Var -= 1  ; Sets Var to be 10.33 with one leading space because the total width is 6.
SetFormat, float, 0.2
Var += 1  ; Sets Var to be 11.33 with no leading spaces.

SetFormat, float, 06.0
Var += 0  ; Sets Var to be  000012

SetFormat, integer, hex
Var += 0  ; Sets Var to be 0xc
SetFormat, integer, d