Question

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

Was it helpful?

Solution

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.

OTHER TIPS

A counter-example can be provided by simply testing random values until a stable maximum value can be found. A possible problem with this approach is that only local maxima can be found and no exhaustive global maximum.

The result on this PC was:

Max. float length: 15 (-1.09045365E-32)

Which is a counter-example to your -Float.MAX_VALUE.

Sometimes it is useful to know what the maximum length is of Float.toString(), in which case this question can be answered.

public class FindMaxStringForPrimitive {

  private static final int MAX_PERIOD = 10000; // Number of iterations before
                                               // concluding answer

  public static void main(String... args) {
    maxFloatLength();
  }

  private static void maxFloatLength() {
    final class Test {
      int    r   = Integer.MIN_VALUE;
      Random rnd = new Random();
      String max = "";

      boolean fit() {
        return fit(Float.intBitsToFloat(rnd.nextInt()));
      }

      boolean fit(float value) {
        String x = Float.toString(value);
        if (x.length() > r) {
          r = x.length();
          max = x;
          return true;
        }
        return false;
      }
    }
    Test test = new Test();
    int i = 0;
    test.fit(Float.MAX_VALUE);
    test.fit(-Float.MAX_VALUE);
    test.fit(Float.MIN_VALUE);
    test.fit(-Float.MIN_VALUE);
    test.fit(Float.NaN);
    test.fit(Float.POSITIVE_INFINITY);
    test.fit(Float.NEGATIVE_INFINITY);
    while (i < MAX_PERIOD)
      if (test.fit()) i = 0;
      else
        i++;
    System.out.println("Max. float length: " + test.r + " (" + test.max + ")");
  }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top