logo

Výstupní formátování Java

Někdy chceme, aby byl výstup programu vytištěn v daném konkrétním formátu. V programovacím jazyce C je to možné pomocí funkce printf(). V této části probereme různé formátování výstupu.

Pojďme diskutovat o tom, jak můžeme formátovat výstup v Javě.

Existují dvě metody, které lze použít k formátování výstupu v Javě:

co je rom
  • Pomocí metody printf().
  • Pomocí metody format( ).

Formátování výstupu pomocí metody System.out.printf( ).

Implementace této metody je velmi snadná, protože je podobná funkci printf() v programování v jazyce C.

FormattedOutput1.java

 public class FormattedOutput1 { public static void main( String args[ ] ) { // printing the string value on the console String str = ' JavaTpoint ' ; System.out.printf( ' 
 Printing the String value : %s 
 ', str ) ; // printing the integer value on the console int x = 512 ; System.out.printf( ' 
 Printing the integer value : x = %d 
 ', x ) ; // printing the decimal value on the console float f = 5.25412368f ; System.out.printf( ' 
 Printing the decimal value : %f 
 ', f ) ; // this formatting is used to specify the width un to which the digits can extend System.out.printf( ' 
 Formatting the output to specific width : n = %.4f 
 ', f ) ; // this formatting will print it up to 2 decimal places System.out.printf( ' 
 Formatted the output with precision : PI = %.2f 
 ', f ) ; // here number is formatted from right margin and occupies a width of 20 characters System.out.printf( ' 
 Formatted to right margin : n = %20.4f 
 ', f ) ; } } 

Výstup:

 Printing the String value : JavaTpoint Printing the integer value : x = 512 Printing the decimal value : 5.254124 Formatting the output to specific width : n = 5.2541 Formatted the output with precision : PI = 5.25 Formatted to right margin : n = 5.2541 

System.out.format() je ekvivalentní funkci printf() a lze ji také použít.

Důležité je poznamenat, že System.out.print() a System.out.println() přebírají jeden argument, ale metoda printf() může přijmout více argumentů.

algoritmy binární vyhledávání

Formátování pomocí třídy DecimalFormat:

DecimalFormat se používá k formátování desetinných čísel.

FormattedOutput2.java

 import java.text.DecimalFormat ; // definition of the class public class FormattedOutput2 { public static void main( String args[ ] ) { double x = 123.4567 ; // printing the number System.out.printf( ' 
 The number is : %f 
 ', x ) ; // printing only the numeric part of the floating number DecimalFormat ft = new DecimalFormat( ' #### ' ) ; System.out.println( ' 
 Without fraction part the number is : ' + ft.format( x ) ) ; // printing the number only upto 2 decimal places ft = new DecimalFormat( ' #.## ' ) ; System.out.println( ' 
 Formatted number with the specified precision is = ' + ft.format( x ) ) ; // automatically appends zero to the rightmost part of decimal, instead of #, we use digit 0 ft = new DecimalFormat( ' #.000000 ' ) ; System.out.println( ' 
 Appending the zeroes to the right of the number = ' + ft.format( x ) ) ; // automatically appends zero to the leftmost of decimal number instead of #, we use digit 0 ft = new DecimalFormat( ' 00000.00 ' ) ; System.out.println( ' 
 Appending the zeroes to the left of the number = '+ ft.format( x ) ) ; // formatting money in dollars double income = 550000.789 ; ft = new DecimalFormat( ' $###,###.## ' ) ; System.out.println( ' 
 Your Formatted Income in Dollars : ' + ft.format( income ) ) ; } } 

Výstup:

 The number is : 123.456700 Without fraction part the number is : 123 Formatted number with the specified precision is = 123.46 Appending the zeroes to the right of the number = 123.456700 Appending the zeroes to the left of the number = 00123.46 Your Formatted Income in Dollars : 0,000.79 

Specifikátory formátu řetězce Java

Zde poskytujeme tabulku specifikátorů formátu podporovaných Java String.

programování v c polích
Specifikátor formátu Datový typ Výstup
%A plovoucí desetinná čárka (kromě BigDecima l) Vrátí hexadecimální výstup čísla s plovoucí desetinnou čárkou.
%b Jakýkoliv typ ' true ' pokud není null, ' false ' pokud null
%C Charakter Unicode znak
%d celé číslo ( vč. byte, short, int, long, bigint ) Desetinné celé číslo
%To je plovoucí bod Desetinné číslo ve vědeckém zápisu
%F plovoucí bod Desetinné číslo
%G plovoucí bod Desetinné číslo, případně ve vědeckém zápisu v závislosti na přesnosti a hodnotě.
%h jakýkoliv typ Hexadecimální řetězec hodnoty z metody hashCode().
%n Žádný Oddělovač řádků specifický pro platformu.
celé číslo ( vč. byte, short, int, long, bigint ) Osmičkové číslo
%s jakýkoliv typ Hodnota řetězce
%t Datum/čas (včetně délky, kalendáře, data a TemporalAccessor) %t je předpona pro převod data/času. Poté je potřeba více příznaků formátování. Viz převod data/času níže.
%X celé číslo ( vč. byte, short, int, long, bigint ) Hexadecimální řetězec.