Toisinaan haluamme, että ohjelman tuloste tulostetaan tietyssä muodossa. C-ohjelmointikielessä tämä on mahdollista printf( ) -funktiolla. Tässä osiossa käsittelemme erilaisia tulosteen muotoiluja.
Keskustellaan siitä, kuinka voimme muotoilla tulosteen Javassa.
Tulosteen muotoiluun Javassa voidaan käyttää kahta tapaa:
mac-käyttöjärjestelmät
- Käytä printf( ) -menetelmää
- Formaatti( ) -menetelmän käyttäminen
Tulosteen muotoilu System.out.printf( ) -menetelmällä
Tämän menetelmän toteuttaminen on erittäin helppoa, koska se on samanlainen kuin C-ohjelmoinnin printf()-funktio.
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 ) ; } } Lähtö:
sivustot, kuten bedpage
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( ) on sama kuin printf( ) ja sitä voidaan myös käyttää.
Tärkeä huomautus on, että System.out.print( ) ja System.out.println( ) ottavat yhden argumentin, mutta printf( ) -menetelmä voi hyväksyä useita argumentteja.
Muotoilu käyttämällä DecimalFormat-luokkaa:
DecimalFormatia käytetään desimaalilukujen muotoiluun.
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 ) ) ; } } Lähtö:
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-merkkijonomuotomääritykset
Tässä tarjoamme taulukon Java-merkkijonon tukemista muotomäärityksistä.
manuaalinen testaus
| Muotomäärittäjä | Tietotyyppi | Lähtö |
|---|---|---|
| %a | liukuluku (paitsi BigDecima l) | Palauttaa liukulukuluvun heksadesimaalituloksen. |
| %b | Minkä tahansa tyyppinen | ' tosi ' jos ei ole nolla, ' false ' jos nolla |
| %c | Merkki | Unicode-merkki |
| %d | kokonaisluku (sis. tavu, lyhyt, int, pitkä, iso) | Desimaalikokonaisluku |
| %Se on | liukuluku | Desimaaliluku tieteellisessä merkinnässä |
| %f | liukuluku | Desimaaliluku |
| %g | liukuluku | Desimaaliluku, mahdollisesti tieteellisessä muodossa tarkkuudesta ja arvosta riippuen. |
| %h | minkä tahansa tyyppinen | HashCode( ) -menetelmän arvon heksadesimaaliluku. |
| %n | Ei mitään | Alustakohtainen rivierotin. |
| %O | kokonaisluku (sis. tavu, lyhyt, int, pitkä, iso) | Oktaaliluku |
| %s | minkä tahansa tyyppinen | Merkkijonon arvo |
| %t | Päivämäärä/aika (sis. pitkä, kalenteri, päivämäärä ja temporalAccessor) | %t on päivämäärä/aika-muunnosten etuliite. Tämän jälkeen tarvitaan lisää muotoilulippuja. Katso päivämäärän ja kellonajan muunnos alla. |
| %x | kokonaisluku (sis. tavu, lyhyt, int, pitkä, iso) | Hex merkkijono. |