User Tools

Site Tools


Sidebar

dictionary:more:formatnumber

format number

Formats a number in a specific way given the format string. Similar to the format() command. This same format is supported for both the format number verb and the format string parameter of a Unit that controls it’s display of a numerical value.

The formatting syntax is also used to format the outgoing message for a numerical value going to an MQTT server via the MQTT Plugin.

Usage:

format number (number, the number that gets formatted) format (text, the format string, see below)

format string spec:

# Placeholder that displays the digit from the value if it is present. If fewer placeholder characters are used than in the passed number, then the result is rounded.
0 Placeholder that displays the digit from the value if it is present. If no digit is present, 0 (Zero) is displayed in it’s place. (say you wanted a month that was always zero padded then you’d pass the month number with a format string of “00”)
. Placeholder for the position of the decimal point.
, Placeholder that indicates the number should be formatted with thousands separators.
% displays the number multiplied by 100.
( displays an open paren.
) displays a closed paren.
+ displays the plus sign to the left of the number if the number is positive or the minus sign if negative.
- displays the minus sign to the left of the number if the number is negative, makes no change if the number is positive.
E or e displays the number in scientific notation.
\character displays the character that follows the backslash. When using with the verb in applescript you will need to double slash this. See applescript usage note below.

additionally the string may contain 3 formats separated by semicolons to be used when the number is positive, negative or zero.

Examples:

FormatNumberFormatted String
#.##1.7861.79
#.00001.31.3000
000050005
#.#\ \°\F75.62175.6 °F
#%0.2525%
###,###.##145678.5145,678.5
#.##e145678.51.46e+5
-#.##-3.7-3.7
+#.##3.7+3.7
#.##;(#.##);\z\e\r\o3.73.7
#.##;(#.##);\z\e/r\o-3.7(3.7)
#.##;(#.##);\z\e\r\o0zero

AppleScript Usage Note: the same backslash character is used to escape values in an applescript string, so to use this in AppleScript you need to double backslash the backslash character. For the example above of formatting a temperature value which is “#.#\ \°\F” would work in the various formatting fields but when using in the format number verb in AppleScript you would have to enter it like:

write log format number 75.621 format “#.#\\ \\°\\F”
dictionary/more/formatnumber.txt · Last modified: 2023/01/09 13:46 by James Sentman