Java 17 Recipes


-2. Formatting Double and Long Decimal Values


Download 3.2 Mb.
Pdf ko'rish
bet67/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   63   64   65   66   67   68   69   70   ...   245
Bog'liq
Java 17 Recipes

4-2. Formatting Double and Long Decimal Values
 Problem
You need to format double and long numbers in your application.
 Solution
Use the DecimalFormat class to format and round the value to the precision your 
application requires. In the following method, a double value is accepted and a 
formatted string value is printed.
public static void formatDouble(double myDouble){
NumberFormat numberFormatter = new DecimalFormat("##.000");
String result = numberFormatter.format(myDouble);
System.out.println(result);
}
The main class is:
public static void main(String[] args) {
formatDouble(Double.valueOf("345.9372"));
}
For instance, if the double value passed into the formatDouble() method is 345.9372, 
the result is 345.937. Similarly, if .7697 is passed to the method, the result is .770.
Each of the results is formatted using the specified pattern and then rounded 
accordingly.
 How It Works
The DecimalFormat class can be used along with the NumberFormat class to round
and/or format double or long values. NumberFormat is an abstract class that provides the 
interface for formatting and parsing numbers. This class provides the ability to format 
and parse numbers for each locale and obtain formats for currency, percentage, integers, 
and numbers. By itself, the NumberFormat class can be very useful as it contains factory 
methods that obtain formatted numbers. In fact, little work needs to be done to obtain 
a formatted string. For example, the following code demonstrates calling some factory 
methods on the NumberFormat class.
Chapter 4 Numbers aNd dates


111
// Obtains an instance of NumberFormat class
NumberFormat format = NumberFormat.getInstance();
// Format a double value for the current locale
String result = format.format(83.404);
System.out.println(result);
// Format a double value for an Italian locale
result = format.getInstance(Locale.ITALIAN).format(83.404);
System.out.println(result);
// Parse a String into a Number
try {
Number num = format.parse("75.736");
System.out.println(num);
} catch (java.text.ParseException ex){
System.out.println(ex);
}
The output is:
Current Locale: 83,404
Italian Locale: 83,404
Now a number: 75736
To format using a pattern, the DecimalFormat class can be used along with 
NumberFormat. In the solution to this recipe, you saw that creating a new DecimalFormat 
instance by passing a pattern to its constructor would return a NumberFormat type. This 
is because DecimalFormat extends the NumberFormat class. Because the NumberFormat 
class is abstract, DecimalFormat contains all the functionality of NumberFormat, plus 
added functionality for working with patterns. Therefore, it can work with different 
formats from the locales, just as you have seen in the previous demonstration. This 
provides the ultimate flexibility when working with double or long formatting.
As mentioned previously, the DecimalFormat class can take a string-based pattern 
in its constructor. You can also use the applyPattern() method to apply a pattern to 
the Format object after the fact. Each pattern contains a prefix, numeric part, and suffix, 
allowing you to format a particular decimal value to the required precision and include 
leading digits and commas. Each of the patterns also contains a positive and negative 
subpattern. A semicolon separates these two subpatterns (;), and the negative subpattern 
is optional. If there is no negative subpattern present, the localized minus sign is used. For 
instance, a complete pattern example would be ###,##0.00;(###,##0.00).
Chapter 4 Numbers aNd dates


112
Note the constructor double(string), Float(float), Integer(int), Long(long), 
Character(char), short(short), byte(byte) and boolean(boolean) have been 
deprecated since version 9 and marked for removal. It is possible to use to use
Float floatValue = 7.82f;
Float floatValue = Float.valueOf("7.82");
instead of
new Double("345.9372");

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   63   64   65   66   67   68   69   70   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling