OutputVar := Mfunc.SetFormat(NumberType, Format)
Sets the format of integers and floating point numbers generated by math operations and returns the previous format.
NumberType
Must be one of the enumeration items of MfSetFormatNumberType. Can be passed in as Instance of MfSetFormatNumberType, MfSetFormatNumberType.Instance.[EnumItem], integer matching enumeration item values of MfSetFormatNumberType or string matching one of the enumeration item names MfSetFormatNumberType.
Format
For NumberType Integer or IntegerFast, specify H or HEX for hexadecimal, or D for decimal. Hexadecimal numbers all start with the prefix 0x (e.g. 0xFF). AutoHotkey [v1.0.90+]: Hexadecimal integers are formatted with digits A-F in lowercase when this parameter is h and uppercase when it is H.
For NumberType Float or FloatFast, specify TotalWidth.DecimalPlaces (e.g. 0.6). In v1.0.46.11+, the letter "e" may be appended to produce scientific notation; e.g. 0.6e or 0.6E (using uppercase produces an uppercase E in each number instead of lowercase). Note: In AutoHotkey 1.x, scientific notation must include a decimal point; e.g. 1.0e1 is valid but not 1e1.
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 remarks) 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, floating point results are displayed as integers rather than a floating point number. The starting default is 6.
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 (e.g. 06.2).
Returns the Format the previous format as string var.
In AutoHotKey [v1.0.48+], IntegerFast may be used instead of Integer, and FloatFast may be used instead of Float.
If the slow mode "Integer" or "Float" is used anywhere in the script, even if that SetFormat line is never executed, the caching of integers or floating point numbers (respectively) is disabled the moment the script launches.
In AutoHotKey [v1.0.48+], floating point variables have about 15 digits of precision internally unless SetFormat Float (i.e. the slow mode) is present anywhere in the script. In that case, the stored precision of floating point numbers is determined by DecimalPlaces (like it was in [AutoHotkye pre-1.0.48] versions). In other words, once a floating point result is stored in a variable, the extra precision is lost and cannot be reclaimed without redoing the calculation with something like Mfunc.SetFormat("Float", 0.15). To avoid this loss of precision, avoid using SetFormat Float anywhere in the script, or use SetFormat FloatFast instead.
Regardless of whether slow or fast mode is in effect, floating point results and variables are rounded off to DecimalPlaces whenever they are displayed or converted to a string of text (e.g. MsgBox or FileAppend). To see the full precision, use something like Mfunc.SetFormat("FloatFast", 0.15).
To convert a floating point number to an integer, use Var:=Round(Var), Var:=Floor(Var), or Var:=Ceil(Var). To convert an integer to a floating point number, add 0.0 to it (e.g. Var+=0.0) or use something like MyFloat:=Round(MyInteger, 1).
The built-in variable A_FormatFloat contains the current floating point format (e.g. 0.6).
Integer results are normally displayed as decimal, not hexadecimal. To switch to hexadecimal, use Mfunc.SetFormat("IntegerFast", "Hex"). This may also be used to convert an integer from decimal to hexadecimal (or vice versa) as shown in the example at the very bottom of this page.
Literal integers specified in a script may be written as either hexadecimal or decimal. Hexadecimal integers all start with the prefix 0x (e.g. 0xA9). They may 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.
AutoHotkey supports 64-bit signed integers, which range from -9223372036854775808 (-0x8000000000000000) to 9223372036854775807 (0x7FFFFFFFFFFFFFFF).
The built-in variable A_FormatInteger contains the current integer format (H or D).
Wrapper for AutoHotkey Docs - SetFormat.
Static method.
If SetFormat 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 these defaults; but the defaults may be changed by using SetFormat in the auto-execute section (top part of the script).
An old-style assignment like x=%y% omits any leading or trailing spaces (i.e. padding). To avoid this, use AutoTrim or the colon-equals operator (e.g. x:=y).
You can determine whether a variable contains a numeric value by using "if var is number/integer/float"
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.
Var := SubStr(" " . Var, -9) ; A single-line alternative to the above two lines.
See Also:AutoHotkey Docs - SetFormat.
Throws MfArgumentNullException if NumberType or Format is null.
Throws MfArgumentException if there are errors with the Parameters.
Throws MfException if there is any other errors.
objFloat := new MfFloat(11.333333) ; default format is 0.6
objFloat.Format := "6.2"
varPreviousFormat := Mfunc.SetFormat(MfSetFormatNumberType.Instance.FloatFast, objFloat.Format) ; set Format for FloatFast
objFloat.Value -=1 ; Sets objFloat.Value to be 10.33 with one leading space because the total width is 6.
Mfunc.SetFormat(4, objFloat.Format) ; set Format for FloatFast
objFloat.Value += 1 ; Sets objFloat.Value to be 11.33 with no leading spaces.
objFloat.Format := "06.0"
Mfunc.SetFormat(4, objFloat.Format) ; Sets format for FloatFast
objFloat.Value += 0 ; Sets objFloat.Value to be 000011
; reset the previous FloatFast format
Mfunc.SetFormat(4, varPreviousFormat)
;-----------------------------------------------------------------------------
Var := 11.333333
varPreviousFormat := Mfunc.SetFormat(MfSetFormatNumberType.Instance.Float, "6.2") ; set Format for Float
Var -= 1 ; Sets Var to be 10.33 with one leading space because the total width is 6.
Mfunc.SetFormat("Float", "6.2") ; sets format for Float
Var += 1 ; Sets Var to be 11.33 with no leading spaces.
Mfunc.SetFormat(3, "06.0") ; Sets format for Float
Var += 0 ; Sets Var to be 000011
; reset the previous Float format
Mfunc.SetFormat(MfSetFormatNumberType.Instance.Float, varPreviousFormat)
; Convert a decimal integer to hexadecimal:
varPreviousFormat := Mfunc.SetFormat(MfSetFormatNumberType.Instance.IntegerFast, "H") ; set Format for IntegerFast
Var += 0 ; Sets Var (which previously contained 11) to be 0xB.
Var .= "" ; Necessary due to the "fast" mode.
Mfunc.SetFormat(MfSetFormatNumberType.Instance.IntegerFast, varPreviousFormat) ; reset to previous format