VAr
VAr

Reputation: 2601

xtype: numberfield value is going to auto correct(change) for more than 16 digit value

can any one explain why(how) the xtype: numberfield value is going to auto correct(change) if am providing more than a 16 digits value. For Example:

  1. 22222222222222222 is changed to 22222222222222224
  2. 222222222222222222 is changed to 222222222222222200
  3. 2222222222222222222 is changed to 2222222222222222300
  4. 22222222222222222222 is changed to 22222222222222220000
  5. 222222222222222222222 is changed to 222222222222222230000
  6. 2222222222222222222222 is changed to 2.2222222222222222e+21
  7. 22222222222222222222222 is changed to 2.2222222222222223e+22

Which results in my page after rendering as shown below when get the value through in my component jsp page NumberFieldTestValue:<%= properties.get("numberfieldname","") %> Resulting as below

NumberFieldTestValue: 2.2222222222222223e+22

Upvotes: 0

Views: 1642

Answers (1)

toniedzwiedz
toniedzwiedz

Reputation: 18563

The problem

This behavior is caused by the fact that the dialog behavior is implemented in JavaScript. The numbers you're having problems with cannot be represented in it.

The language conforms to the ECMASCRIPT 5.1 specification.

To quote the Number type description

all the positive and negative integers whose magnitude is no greater than 2^53 are representable in the Number type

The base 2 logarithm of 2222222222222222222222 is about 70, which means the number exceeds the maximum value. Hence your problems.

All in all, if you check your examples in a plain JS console in a browser, the same behavior will be displayed so this is not really a CQ problem.

Solution 1 - use a different type

To avoid this, you could potentially use xtype="textfield" and validate it against a regular expression to see if it consists of numbers only. Then you'd have to make sure to use a type capable of holding such numbers at the backend (like BigInteger).

The field could look like this:

<numberOfSandGrains
  jcr:primaryType="cq:Widget"
  fieldLabel="Number of grains of sand at the beach"
  name="./grainsCount"
  regex="/\d+/"
  regexText="Please enter a numeric value."
  xtype="textfield"/>

Solution 2 - change scale

Another option is to change the logic behind the configuration and use some different units if applicable. For instance, if the value 2222222222222222222222 is a number of grams (weight of an object/substance), it makes perfect sense to read it in metric tons instead (at least in many cases). The author could probably do without entering such humongous numbers.

You'll need to be extra-careful if you go this way.

Upvotes: 3

Related Questions