Java Reference
In-Depth Information
22.1.3. Floating-Point Conversions
The floating-point conversions apply to arguments of type float and
double . The different conversions are
e , E computerized scientific notation (such as 3.142e+00 )
f , F decimal format (such as 3.142)
g , G general scientific notation (see below)
a , A hexadecimal exponential format (as used for hexadecimal
floating-point literalssee page 168 )
In each case a value of NaN or infinity produces the strings "NaN" or "In-
finity" .
The width value specifies the minimum number of characters to output,
with the result being padded if necessary to fit the requested width. For
everything except general scientific conversion, the precision value spe-
cifies the number of digits to output after the decimal point (or hexa-
decimal point), with the value rounded as necessary to match that pre-
cision. If no precision is given then it defaults to 6, except for the hexa-
decimal exponential form where no precision means to use as many
places as needed.
A conversion to general scientific notation chooses the output format
based on the magnitude of the value and the precision that is given.
In this case precision indicates the number of significant digits (not the
number of fractional digits). If after rounding to the specified precision,
the value is smaller than 10 -4 , or it is greater than or equal to 10 precision ,
then computerized scientific notation is used; otherwise, decimal format
is used. For example, given
System.out.
printf("%1$.5g %1$.4g %1$.3g %1$.2g %n", Math.PI * 100);
 
Search WWH ::




Custom Search