Number and Range
3 min read
Number and Range both store a real number in a numeric column, so you can filter and sort by them (for example "price under 500") without converting text. They behave the same way and differ only in the control: Number is a typed input, Range is a slider.
Number | Range | |
|---|---|---|
Type id |
|
|
Default column |
|
|
Control |
|
|
Safe type changes | Range, Text, Text area, Email, URL, Password | Number, Text, Text area, Email, URL, Password |
Neither has its own Type settings. Use Min value, Max value and Step on the Validation tab (see Settings every field has).
Column type
The default DECIMAL(20,6) holds up to 14 digits before the decimal point and 6 after. In the field's SQL column options you can pick another numeric type: INT, BIGINT, TINYINT, SMALLINT, MEDIUMINT, FLOAT, DOUBLE or DECIMAL(p,s), and tick unsigned to forbid negative numbers. Choose a whole-number type for counts and IDs and DECIMAL for money.
Available column functions: abs (stores the absolute value) and rand (stores a random number between 0 and 1 when the value is empty).
What happens on save
- The value must be numeric. Text such as
abc, an empty box and infinite values save an empty field. Scientific notation (1e3) is accepted. - The number is clamped into the range of the column. For
TINYINT UNSIGNEDthat is 0 to 255, forINTabout plus or minus 2.1 billion. A value outside the column never causes a database error; it is brought to the nearest allowed value. - If Min value or Max value is set, the number is clamped into that range too.
- The database rounds to the column's precision (a whole-number column rounds
12.7to13;DECIMAL(20,6)keeps six decimals).
Step is not enforced on the server. It only guides the browser control. 12 is saved even when the step is 5. Clamp or round in your own code if you need it.
Values below zero need a signed column: if Min value is negative, make sure the SQL column is not unsigned.
What you read back
wpmcf_get_field() returns an int when the stored number is whole and a float otherwise. A DECIMAL column that the database reports as "300.000000" is returned as 300.
Saved | Returned |
|---|---|
|
|
|
|
|
|
|
|
|
|
Template usage
$price = wpmcf_get_field( 'product', 'price' ); // 19.9 (float) or 20 (int) or null
if ( null !== $price ) {
echo esc_html( number_format_i18n( $price, 2 ) ); // 19.90 in your site's locale
}
wpmcf_the_field( 'product', 'price' ); // <span class="wpmcf-field-value">19.9</span>[wpmcf_field group="product" field="price"]The printed value is the plain number without thousands separators or trailing zeros (1234.50 prints as 1234.5). Add a Prefix (for example $) and Suffix in the Display tab for currency or units, or format the number in your template.
Edit screen
- Number: a number input. With no Step set it accepts decimals (
step="any"). Min value, Max value and Step become themin,maxandstepattributes. A number field is empty until the editor types something. - Range: a slider (200 pixels wide in the classic style, styled by the plugin in the modern style). It does not show the current number. Set Min value and Max value: without them the browser uses 0 to 100.
A browser always submits a slider's position, so saving the object stores the slider's current value even if the editor never touched it. If "no answer" must be possible, use a Number field.
Import
CSV imports (for example Pro data sources) accept 1 234,5 style cells and store 1234.5.
Troubleshooting
- My decimals are cut.
DECIMAL(20,6)keeps six decimals; a whole-number column keeps none. Change the SQL type in the field's SQL column options. - A negative number is saved as 0. The column is unsigned. Untick unsigned or pick a signed type. See Field groups.
- A number above my Max value was saved as the maximum. That is the intended clamping.
- The value is
nullin PHP. Nothing was saved, or the text was not numeric.
Still stuck? Contact support