— Scott McNealy, Sun Microsystems Chairman, President and ceo


“%d” , 42); The TYPE is mandatory, everything else is optional. 42



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

“%d”
, 42);
The TYPE is mandatory, everything else is optional.
42
%d 
decimal
format(
“%.3f”
,
 
42.000000);
42.000
%f 
floating point
format(
“%x”
, 42);
2a
%x 
hexadecimal
format(
“%c”, 
42);
*
%c 
character
The argument must be compatible with an int, so that means 
only byte, short, int, and char (or their wrapper types).
The argument must be of a floating point type, so that 
means only a float or double (primitive or wrapper) as well 
as something called BigDecimal (which we don’t look at in 
this book).
A 42.25 would not work! It 
would be the same as trying to 
directly assign a double to an 
int variable.
Here we combined the “f” 
with a precision indicator 
“.3” so we ended up with 
three zeroes.
The argument must be a byte, short, int, long (including 
both primitive and wrapper types), and BigInteger. 
You must include a 
type in your format 
instructions, and if you 
specify things besides 
type, the type must 
always come last. 
Most of the time, 
you’ll probably format 
numbers using either 
“d” for decimal or “f” 
for f loating point.
The argument must be a byte, short, char, or int (including 
both primitive and wrapper types).
The number 42 represents 
the char “*”.


300
 
chapter 10
What happens if I have more than one argument?
Imagine you want a String that looks like this:
“The rank is 20,456,654 out of 100,567,890.24.”
But the numbers are coming from variables. What do you do? You simply add two 
arguments after the format String (first argument), so that means your call to format() 
will have three arguments instead of two. And inside that first argument (the format 
String), you’ll have two different format specifiers (two things that start with “%”). The 
first format specifier will insert the second argument to the method, and the second 
format specifier will insert the third argument to the method. In other words, the 
variable insertions in the format String use the order in which the other arguments are 
passed into the format() method.
int one = 20456654;
double two = 100567890.248907;
String s = String.format(“The rank is 

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   280   281   282   283   284   285   286   287   ...   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