Numbers can be formatted in WS Form using the #number_format
variable.
#number_format
can be used on your form (e.g. in the default value of field) and also in actions (e.g. the Send Email action).
Demo
Syntax
The syntax of the #number_format
variable is as follows:
#number_format(number, decimals, decimal separator, thousands separator)
The parameters are as follow:
Parameter | Example | Description |
---|---|---|
number |
#field(123) or 12345.6789 |
The input number. |
decimals |
2 |
Sets the number of decimal digits. |
decimal_separator |
. |
Sets the separator for the decimal point. |
thousands_separator |
, |
Sets the thousands separator. |
Examples
These examples use a field with ID 123 that has a value of 123456.789
.
Example | Description |
---|---|
#number_format(#field(123)) |
123,457 |
#number_format(#field(123), 2) |
123,456.79 |
#number_format(#field(123), 2, ",", ".") |
123.456,79 |
#number_format(#field(123), 0, ".", " ") |
123 456 |
#number_format(#field(123), 0, ".", " ") kr |
123 456 kr |
For currency formatting we recommend using #ecommerce_price(#field(123))
which automatically outputs a price according to your e-commerce currency settings in WS Form or WooCommerce.
Dynamically Updating with #text
If you want a number to change dynamically if the number parameter changes, you can wrap the variable in #text. For example:
#text(#number_format(#field(123), 2))
This would output the value of field ID 123 to two decimal places, keeping it update if field ID 123 changes.