logo

Java izvades formatēšana

Dažreiz mēs vēlamies, lai programmas izvade tiktu izdrukāta noteiktā formātā. C programmēšanas valodā tas ir iespējams, izmantojot funkciju printf( ). Šajā sadaļā mēs apspriedīsim dažādus izvades formatējumus.

Apspriedīsim, kā mēs varam formatēt izvadi Java.

Ir divas metodes, ko var izmantot, lai formatētu izvadi Java:

izdzēsiet failu java
  • Izmantojot metodi printf( ).
  • Izmantojot formāta( ) metodi

Izvades formatēšana, izmantojot System.out.printf( ) metodi

Šīs metodes ieviešana ir ļoti vienkārša, jo tā ir līdzīga funkcijai printf( ) C programmēšanā.

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 ) ; } } 

Izvade:

 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( ) ir līdzvērtīgs printf( ), un to var arī izmantot.

Svarīgi atzīmēt, ka System.out.print( ) un System.out.println( ) izmanto vienu argumentu, bet metode printf( ) var pieņemt vairākus argumentus.

java apmācības

Formatēšana, izmantojot DecimalFormat klasi:

DecimalFormat tiek izmantots, lai formatētu decimālskaitļus.

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 ) ) ; } } 

Izvade:

 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 

Java virkņu formāta specifikācijas

Šeit mēs piedāvājam formāta specifikāciju tabulu, ko atbalsta Java virkne.

sakārtot pēc nejaušības principa sql
Formāta norādītājs Datu tips Izvade
%a peldošais komats (izņemot BigDecima l) Atgriež peldošā komata skaitļa hex izvadi.
%b Jebkura veida 'patiess', ja nav nulle, 'false', ja nulle
%c Raksturs Unikoda rakstzīme
%d vesels skaitlis (ieskaitot baitu, īsu, int, garu, bigint) Decimālskaitlis
%Tas ir peldošais punkts Decimālskaitlis zinātniskajā apzīmējumā
%f peldošais punkts Decimālskaitlis
%g peldošais punkts Decimālskaitlis, iespējams, zinātniskā apzīmējumā atkarībā no precizitātes un vērtības.
%h jebkura veida Hex vērtības virkne no hashCode( ) metodes.
%n Nav Platformai specifisks līniju atdalītājs.
%O vesels skaitlis (ieskaitot baitu, īsu, int, garu, bigint) Astoņskaitlis
%s jebkura veida Virknes vērtība
%t Datums/laiks (ieskaitot garo, kalendāru, datumu un laika piekļuves ierīci) %t ir datuma/laika reklāmguvumu prefikss. Pēc tam ir nepieciešams vairāk formatēšanas karodziņu. Skatiet tālāk norādīto datuma/laika konvertēšanu.
%x vesels skaitlis (ieskaitot baitu, īsu, int, garu, bigint) Hex virkne.