Title pretty much says it all...
I am trying to enforce the maximum length of text input on a form. One of the fields can be any valid floating-point number. What would its maximum length be?
For example, for an integer
// negative sign makes MIN_VALUE larger than MAX_VALUE
String.valueOf(Integer.MIN_VALUE).length();
UPDATE
I have tested the following:
String.valueOf(-Float.MIN_VALUE).length();
String.valueOf(-Float.MAX_VALUE).length();
String.valueOf( Float.MIN_VALUE).length();
String.valueOf( Float.MAX_VALUE).length();
Which gives me the following output:
8
13
7
12
I'm not convinced that 13 is the maximum length
MAX_VALUE. A constant holding the largest positive finite value of type float , (2-2-23)·2127. It is equal to the hexadecimal floating-point literal 0x1.
The maximum value any floating-point number can be is approx 1.8 x 10308. Any number greater than this will be indicated by the string inf in Python.
A maximum length for a float value doesn't make sense.
If you want the user to enter any float value representable by java you want allow thing like
1000000000000000000000000000000000000000000000000000000000000000
or even
000000000000000000000000000000000000000000000000000000000.00000000000000000000001
Limits for input fields should be based on business needs not on rules like "I have a limit on all other fields".
The "business" rule here so far seems to be "Can be parsed and stored into a float"
Also note that limiting the input length often prevents input (via cut&paste) of stuff that is longer and only becomes valid input after some editing. So it actually reduces usability.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With