— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet281/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   277   278   279   280   281   282   283   284   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

296
 
chapter 10
the 
format() 
method 
The percent (%) says, “insert argument here” 
(and format it using these instructions)
The first argument to a format() method is called the format String, and it 
can actually include characters that you just want printed as-is, without extra 
formatting. When you see the % sign, though, think of the percent sign as a 
variable that represents the other argument to the method.
format(“I have %.2f bugs to fix.”, 476578.09876);
Output
Argument to be 
formatted.
Format specifiers for the 
second argument to the 
method (the number). 
Characters to include in 
the final String returned 
from format().
I have 476578.10 bugs to fix.
More characters to 
include in the String after 
the second argument is 
formatted and inserted.
The “%” sign tells the formatter to insert the other method argument (the 
second argument to format(), the number) here, AND format it using the 
“.2f” characters after the percent sign. Then the rest of the format String, 
“bugs to fix”, is added to the final output.
Notice we lost some of the numbers 
after the decimal point. Can you guess 
what the “.2f” means?
Adding a comma
format(“I have 
%,.2f
bugs to fix.”, 476578.09876);
I have 476,578.10 bugs to fix.
By changing the format instructions 
from “%.2f” to %,.2f”, we got a 
comma in the formatted number.


numbers 
and
statics
you are here
4
297
The format String uses its 
own little language syntax
You obviously can’t put just anything after the “%” 
sign. The syntax for what goes after the percent 
sign follows very specific rules, and describes 
how to format the argument that gets inserted at 
that point in the result (formatted) String.
You’ve already seen two examples:

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   277   278   279   280   281   282   283   284   ...   555




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

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish