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

NumberUtils.createNumber(final String str) Precision will be lost

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 3.3.2
    • Fix Version/s: 3.5
    • Component/s: lang.math.*
    • Labels:
      None
    • Environment:

      windows 7

      Description

      With commons-lang 3.2.2:
      NumberUtils.createNumber("-160952.54");
      The result is "-160952.55".

      Should not be based on the length of the decimal point number to judge whether the floating point number.
      Using the method (createFloat(str)) of dealing with the valid number greater than seven Numbers will cause accuracy loss.
      The source code is as follows:

      try {
                  if(numDecimals <= 7){// If number has 7 or fewer digits past the decimal point then make it a float
                      final Float f = createFloat(str);
                      if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) {
                          return f;
                      }
                  }
              } catch (final NumberFormatException nfe) { // NOPMD
                  // ignore the bad number
              }
              try {
                  if(numDecimals <= 16){// If number has between 8 and 16 digits past the decimal point then make it a double
                      final Double d = createDouble(str);
                      if (!(d.isInfinite() || (d.doubleValue() == 0.0D && !allZeros))) {
                          return d;
                      }
                  }
              } catch (final NumberFormatException nfe) { // NOPMD
                  // ignore the bad number
              }
      
              return createBigDecimal(str);
          }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                pascalschumacher Pascal Schumacher
                Reporter:
                sydng sydng
              • Votes:
                0 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: