— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

wrapper 
methods
(We’ll talk about Exceptions in the 
next chapter.)


numbers 
and
statics
you are here
4
293
And now in reverse... turning a 
primitive number into a String
There are several ways to turn a number into a String. 
The easiest is to simply concatenate the number to an 
existing String.
double d = 42.5;
String doubleString = “” + d;
double d = 42.5;
String doubleString = Double.toString(d);
Remember the ‘+’ operator is overloaded 
in Java (the only overloaded 
operator) as a 
String concatenator. Anything added to a 
String becomes Stringified.
Another way to do it using a static 
method in class Double.
Yeah, 
but how do I make it 
look like money? With a dollar 
sign and two decimal places 
like $56.87 or what if I want 
commas like 45,687,890 or 
what if I want it in...
Where’s my printf 
like I have in C? Is 
number formatting part of 
the I/O classes?


294
 
chapter 10
Number formatting
In Java, formatting numbers and dates doesn’t have to be coupled with I/O. Think 
about it. One of the most typical ways to display numbers to a user is through a 
GUI. You put Strings into a scrolling text area, or maybe a table. If formatting was 
built only into print statements, you’d never be able to format a number into a nice 
String to display in a GUI. Before Java 5.0, most formatting was handled through 
classes in the java.text package that we won’t even look at in this version of the 
book, now that things have changed.
In Java 5.0, the Java team added more powerful and flexible formatting through a 
Formatter class in java.util. But you don’t need to create and call methods on the 
Formatter class yourself, because Java 5.0 added convenience methods to some of 
the I/O classes (including printf()) and the String class. So it’s a simple matter of 
calling a static String.format() method and passing it the thing you want formatted 
along with formatting instructions. 
Of course, you do have to know how to supply the formatting instructions, and 
that takes a little effort unless you’re familiar with the printf() function in C/C++. 
Fortunately, even if you
 don’t know printf() you can simply follow recipes for the 
most basic things (that we’re showing in this chapter). But you will want to learn 
how to format if you want to mix and match to get anything you want. 
We’ll start here with a basic example, then look at how it works. (Note: we’ll revisit 
formatting again in the I/O chapter.)
public class TestFormats {
public static void main (String[] args) {
String s = 

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   275   276   277   278   279   280   281   282   ...   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