Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4279802

RFE: NumberFormat.getNumberOfDigits() method

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P5 P5
    • None
    • 1.3.0
    • core-libs
    • generic
    • generic



      Name: krT82822 Date: 10/09/99


      This proposed method delivers functionality not strictly offered by any of the existing methods in the NumberFormat class. The problem is this: when I want to format the display of numbers, I want to know how many digits certain numbers will have and be able to 'pretty-print.' Since variables have an arbitrary number of digits based on the run-time data they contain (up to the limits of their declared type, of course), there must be an easy way to accomplish this. Well, there is. By passing the number as a parameter to a method called getNumberOfDigits, the programmer will be returned the number of digits. Here are the method signatures:

      [may want to return int vs. byte?]

      <visibility modifiers> byte getNumberOfDigits (byte n)
      <visibility modifiers> byte getNumberOfDigits (short n)
      <visibility modifiers> byte getNumberOfDigits (int n)
      <visibility modifiers> byte getNumberOfDigits (long n)
      {
         byte digitCount; // number of digits in integer

         while ((n / 10) > 0)
         {
            digitCount++;
            n /= 10; // idiv number by 10
         }
         return digitCount;
      }

      This is a method that could be used by many programmers to beautify their output of numbers on screen, in hardcopy print-outs, et cetera. It's a very trivial, but overlooked, method.
      (Review ID: 96314)
      ======================================================================

            nlindenbsunw Norbert Lindenberg (Inactive)
            kryansunw Kevin Ryan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: