Compact Number Parsing

The parse() method of the compact number formatter can be used to parse a string that contains a compact form to a numerical value.

Click here to view code image

try {
  System.out.println(shortCompactFormat.parse(“9M”));        // 9000000
  System.out.println(longCompactFormat.parse(“9 million”));  // 9000000
} catch (ParseException pe) {
  System.out.println(pe);
}

The compact number formatters above are used to parse different compact forms to numerical values, as shown in Table 18.8. Note that parsing requires that the compact form ends in an appropriate suffix; otherwise, the suffix is ignored, as we can see in Table 18.8.

Table 18.8 Parsing Compact Form to Numbers

Compact form string sNumber returned by shortCompactFormatter.parse(s) methodNumber returned by longCompactFormatter.parse(s) method
“9M”
“9.5M”
“2K”
“1.5K”
“999”

9000000
9500000
2000
1500
999

9
9.5
2
1.5
999

“9 million”
“9.5 million”
“2 thousand”
“1.5 thousand”

9
9.5
2
1.5

9000000
9500000
2000
1500

Specifying the Number of Digits

The following methods of the NumberFormat abstract class and its subclass Decimal-Format allow formatting of numbers to be further refined by setting the number of digits to be allowed in the integral and the decimal part of a number. This also applies for BigDecimal numbers. However, a concrete number formatter can enforce certain limitations on these bounds. In addition, a rounding mode can be set as explained below.

Click here to view code image

void setMinimumIntegerDigits(int n)
int  getMinimumIntegerDigits()
void setMaximumIntegerDigits(int n)
int  getMaximumIntegerDigits()
void setMinimumFractionDigits(int n)
int  getMinimumFractionDigits()
void setMaximumFractionDigits(int n)
int  getMaximumFractionDigits()

Set or get the minimum or maximum number of digits to be allowed in the integral or decimal part of a number.

Click here to view code image

void setRoundingMode(RoundingMode roundingMode)

Sets the rounding mode used in this number formatter—that is, how the resulting value is rounded by the formatter. The enum type java.math.RoundingMode defines constants for such modes (see Table 18.9).

Table 18.9 Selected Rounding Modes

Enum type java.math.RoundingMode constantsDescription
CEILINGRounds toward positive infinity.  1.1 ->  2
(same as
 UP)
-1.8 -> -1
(same as
 DOWN)

FLOORRounds toward negative infinity.  1.8 ->  1 
(same as
 DOWN)
-1.1 -> -2 
(same as
 UP)

UPRounds away from zero. Never decreases the magnitude of the calculated value.  1.1 ->  2
-1.1 -> -2

DOWNRounds toward zero. Never increases the magnitude of the calculated value.  1.8 ->  1
-1.8 -> -1

HALF_UPRounds toward the nearest neighboring value, unless both neighboring values are equidistant, in which case it rounds up. This is the same as normal rounding. Click here to view code image  1.5 ->  2 (same as UP, if discarded fraction is >= 0.5)
 1.4 ->  1 (same as DOWN, if discarded fraction is < 0.5)
-1.4 -> -1 (same as DOWN, if discarded fraction is < 0.5)
-1.5 -> -2 (same as UP, if discarded fraction is >= 0.5)

HALF_DOWNRounds toward the nearest neighboring value, unless both neighboring values are equidistant, in which case it rounds down. Click here to view code image  1.6 ->  2 (same as UP, if discarded fraction is > 0.5)
 1.5 ->  1 (same as DOWN, if discarded fraction is <= 0.5)
-1.5 -> -1 (same as DOWN, if discarded fraction is <= 0.5)
-1.6 -> -2 (same as UP, if discarded fraction is > 0.5)

HALF_EVENRounds toward the nearest neighboring value, unless both neighboring values are equidistant, in which case it rounds toward the even neighbor. This rounding policy is used in floating-point arithmetic in Java. Click here to view code image  2.5 ->  2 (same as HALF_DOWN, if digit left of discarded fraction is even)
 1.5 ->  2 (same as HALF_UP, if digit left of discarded fraction is odd)
-1.5 -> -2 (same as HALF_UP, if digit left of discarded fraction is odd)
-2.5 -> -2 (same as HALF_DOWN, if digit left of discarded fraction is even)

Example 18.5 demonstrates the rounding modes defined by the enum type java.math.RoundingMode. The examples in Table 18.9 are computed in Example 18.5. A number format is created at (1) for the US locale. The maximum number of digits in the fraction part is set to 0 at (2); that is, the floating-point value will be rounded to an integer before formatting.

The method roundIt() at (3) does the rounding and the formatting when passed a formatter, the maximum number of digits (0) required in the fraction part, the rounding mode, and the two values to round and format. The method sets the maximum number of digits required in the fraction part and the rounding mode at (4) and (5), respectively, in the formatter. The formatting of the values is done by calling the format() method at (6) and (7). We encourage checking the output against the rules for rounding for each mode shown in Table 18.9.

Example 18.5 Rounding Modes

Click here to view code image

import java.math.RoundingMode;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
public class Rounding {
  public static void main(String[] args) {
    System.out.println(” Rounding:   v1          v2″);
    NumberFormat nfmtUS = NumberFormat.getNumberInstance(Locale.US);     // (1)
    int maxFractionDigits = 0;                                           // (2)
    roundIt(nfmtUS, maxFractionDigits, RoundingMode.CEILING,   1.1, -1.8);
    roundIt(nfmtUS, maxFractionDigits, RoundingMode.FLOOR,     1.8, -1.1);
    roundIt(nfmtUS, maxFractionDigits, RoundingMode.UP,        1.1, -1.1);
    roundIt(nfmtUS, maxFractionDigits, RoundingMode.DOWN,      1.8, -1.8);
    roundIt(nfmtUS, maxFractionDigits, RoundingMode.HALF_UP,   1.5, 1.4);
    roundIt(nfmtUS, maxFractionDigits, RoundingMode.HALF_UP,   -1.4, -1.5);
    roundIt(nfmtUS, maxFractionDigits, RoundingMode.HALF_DOWN, 1.6, 1.5);
    roundIt(nfmtUS, maxFractionDigits, RoundingMode.HALF_DOWN, -1.5, -1.6);
    roundIt(nfmtUS, maxFractionDigits, RoundingMode.HALF_EVEN, 2.5, 1.5);
    roundIt(nfmtUS, maxFractionDigits, RoundingMode.HALF_EVEN, -1.5, -2.5);
  }
  static void roundIt(NumberFormat nf, int maxFractionDigits, RoundingMode rMode,
                      double v1, double v2) {                  // (3)
    nf.setMaximumFractionDigits(maxFractionDigits);            // (4)
    nf.setRoundingMode(rMode);                                 // (5)
    System.out.printf(“%9s: “, rMode);
    System.out.printf(“%5s -> %2s “, v1, nf.format(v1));       // (6)
    System.out.printf(“%5s -> %2s%n”, v2, nf.format(v2));      // (7)
  }
}

Output from the program:

Click here to view code image

 Rounding:   v1          v2
  CEILING:   1.1 ->  2  -1.8 -> -1
    FLOOR:   1.8 ->  1  -1.1 -> -2
       UP:   1.1 ->  2  -1.1 -> -2
     DOWN:   1.8 ->  1  -1.8 -> -1
  HALF_UP:   1.5 ->  2   1.4 ->  1
  HALF_UP:  -1.4 -> -1  -1.5 -> -2
HALF_DOWN:   1.6 ->  2   1.5 ->  1
HALF_DOWN:  -1.5 -> -1  -1.6 -> -2
HALF_EVEN:   2.5 ->  2   1.5 ->  2
HALF_EVEN:  -1.5 -> -2  -2.5 -> -2

Leave a Reply

Your email address will not be published. Required fields are marked *