Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-1444

NumberUtils.createNumber() does not create BigDecimal for decimal fractions tending to zero

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 3.8.1
    • Fix Version/s: None
    • Component/s: lang.math.*
    • Labels:
      None

      Description

      The following code demonstrates the issue:

      System.out.println(NumberUtils.createNumber("1.1").getClass().getName());
      System.out.println(NumberUtils.createNumber("1.001").getClass().getName());
      System.out.println(NumberUtils.createNumber("1.00001").getClass().getName());
      System.out.println(NumberUtils.createNumber("1.0000001").getClass().getName());
      System.out.println(NumberUtils.createNumber("1.000000001").getClass().getName());
      System.out.println(NumberUtils.createNumber("1.00000000001").getClass().getName());
      System.out.println(NumberUtils.createNumber("1.0000000000001").getClass().getName());
      System.out.println(NumberUtils.createNumber("1.000000000000001").getClass().getName());
      System.out.println(NumberUtils.createNumber("1.00000000000000001").getClass().getName());
      System.out.println(NumberUtils.createNumber("1.0000000000000000001").getClass().getName());
      System.out.println(NumberUtils.createNumber("1.000000000000000000001").getClass().getName());
      System.out.println(NumberUtils.createNumber("1.00000000000000000000001").getClass().getName());

      will print:

      java.lang.Float
      java.lang.Float
      java.lang.Float
      java.lang.Float
      java.lang.Double
      java.lang.Double
      java.lang.Double
      java.lang.Double
      java.lang.Float
      java.lang.Float
      java.lang.Float
      java.lang.Float

      It seems the problem is towards the bottom of the createNumber method that compares the float to double string representation:

      f.toString().equals(d.toString())

      For the misbehaving tests, the string "1.0".equals("1.0")

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              costat Costa Theodosiou
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: