Stay organized with collections
Save and categorize content based on your preferences.
A number field stores a number as its value, and a string as its text. Its value
is always a valid number as defined by the constraints given to
the field at creation; its text could be any string entered into its editor.
The value should cast to a number. If it does not 0 will be used.
Serialization
JSON
The JSON for a number field looks like so:
{"fields":{"FIELDNAME":0}}
Where FIELDNAME is a string referencing a number field, and
the value is the value to apply to the field. The value
follows the same rules as the constructor value.
XML
The XML for a number field looks like so:
<fieldname="FIELDNAME">0</field>
The field node's name attribute contains a string referencing a number
field, and the node's inner text is the value to apply to the field. The
inner text value follows the same rules as the constructor value.
Constraints
Constraints can be set in the field definition, or by using the
setConstraints
function.
Minimum value
The min value sets the smallest/most-negative value the field is allowed to
contain.
Maximum value
The max value sets the largest/most-positive value the field is allowed to
contain.
Rounding
The precision rounds the value to the nearest multiple of precision. This can be
used to make the field only accept multiples of .01, 10, 42, etc.
Common constraints
Positive numbers
To force your field to only accept positive numbers, set the min value to
1.
Integers
To force your field to only accept integers, set the precision to 1.
Creating a number validator
A number field's value is a number, so any validators must accept a number and
return a number, null, or undefined.
Here is an example of a validator that changes the value to be either 0 or 1
depending on if the value was odd or even.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-06-17 UTC."],[[["\u003cp\u003eA number field stores numeric data using \u003ccode\u003evalue\u003c/code\u003e for the number itself and \u003ccode\u003etext\u003c/code\u003e for its string representation, adhering to specified constraints.\u003c/p\u003e\n"],["\u003cp\u003eNumber fields can be created through JSON or JavaScript, allowing customization of initial values, minimums, maximums, and precision.\u003c/p\u003e\n"],["\u003cp\u003eSerialization of number fields is supported in JSON and XML, facilitating data storage and retrieval.\u003c/p\u003e\n"],["\u003cp\u003eConstraints like minimum/maximum values, rounding precision, and custom validators can be applied to control the allowed numeric input.\u003c/p\u003e\n"],["\u003cp\u003eNumber field validators, accepting and returning numeric values, enable enforcing specific data formats or ranges, such as limiting input to even or odd numbers.\u003c/p\u003e\n"]]],["A number field stores a numerical `value` and a string `text`. The `value` is a valid number, while the `text` is any string. Fields are created via JSON or JavaScript, defining `value`, optional `min`, `max`, and `precision`. Constraints set the allowed range and rounding. Serialization uses JSON or XML to represent the field's `value`. Validators can be defined, accepting a number and returning a number, `null`, or `undefined`, allowing for custom logic like enforcing even or odd numbers.\n"],null,["A number field stores a number as its `value`, and a string as its `text`. Its `value`\nis always a valid number as defined by the [constraints](#constraints) given to\nthe field at creation; its text could be any string entered into its editor.\n\nNumber field\n\nNumber field with editor open\n\nNumber field on collapsed block\n\nCreation \n\nJSON \n\n {\n \"type\": \"example_number\",\n \"message0\": \"number: %1\",\n \"args0\": [\n {\n \"type\": \"field_number\",\n \"name\": \"FIELDNAME\",\n \"value\": 100,\n \"min\": 0,\n \"max\": 100,\n \"precision\": 10\n }\n ]\n }\n\nJavaScript \n\n Blockly.Blocks['example_number'] = {\n init: function() {\n this.appendDummyInput()\n .appendField(\"number:\")\n .appendField(new Blockly.FieldNumber(100, 0, 100, 10), 'FIELDNAME');\n }\n };\n\nThe number constructor takes in the following:\n\n- an optional `value`\n- an optional [min](#minimum_value)\n- an optional [max](#maximum_value)\n- an optional [precision](#rounding)\n- an optional [validator](#creating_a_number_validator)\n\nThe `value` should cast to a number. If it does not 0 will be used.\n\nSerialization \n\nJSON\n\nThe JSON for a number field looks like so: \n\n {\n \"fields\": {\n \"FIELDNAME\": 0\n }\n }\n\nWhere `FIELDNAME` is a string referencing a number field, and\nthe value is the value to apply to the field. The value\nfollows the same rules as the constructor value.\n\nXML\n\nThe XML for a number field looks like so: \n\n \u003cfield name=\"FIELDNAME\"\u003e0\u003c/field\u003e\n\nThe `field` node's `name` attribute contains a string referencing a number\nfield, and the node's inner `text` is the `value` to apply to the field. The\ninner text value follows the same rules as the constructor value.\n\nConstraints\n\nConstraints can be set in the field definition, or by using the\n[setConstraints](/blockly/reference/js/Blockly.FieldNumber#setConstraints)\nfunction.\n\nMinimum value\n\nThe `min` value sets the smallest/most-negative value the field is allowed to\ncontain.\n\nMaximum value\n\nThe `max` value sets the largest/most-positive value the field is allowed to\ncontain.\n\nRounding\n\nThe `precision` rounds the value to the nearest multiple of precision. This can be\nused to make the field only accept multiples of .01, 10, 42, etc.\n\nCommon constraints\n\nPositive numbers\n\nTo force your field to only accept positive numbers, set the `min` value to\n1.\n\nIntegers\n\nTo force your field to only accept integers, set the `precision` to 1.\n\nCreating a number validator **Note:** For information on validators in general see [Validators](../validators).\n\nA number field's value is a number, so any validators must accept a `number` and\nreturn a `number`, `null`, or `undefined`.\n\nHere is an example of a validator that changes the value to be either 0 or 1\ndepending on if the value was odd or even. \n\n function(newValue) {\n return newValue % 2;\n }"]]