Java Reference
In-Depth Information
Our examples use locale "be_BY" . Then do the following.
First, create an instance of class NumberFormat with locale as the argument
of the constructor:
NumberFormat convertD= NumberFormat.getInstance(locale)
Then, use instance method convertD.format to convert numbers to strings
using the conventions of locale locale , e.g.
convertD.format(123456543) yields the string "123 456,543"
To create an instance of NumberFormat whose format function converts
currencies (amounts of money) of locale locale , use function getCurrencyIn-
stance ; the example shown below appears to have some unprintable characters:
NumberFormat convertC=
convertC.format(43.56) yields the string "???43,56"
To create an instance of NumberFormat whose format function converts
percents using conventions of locale locale , use function getPercent-
Instance :
NumberFormat convertP=
convertP.format(43.56) yields the string "4 356%"
Random numbers
About random numbers
It is often useful in a computer program to generate and use “random” num-
bers. For example, if you have written a game that uses a deck of cards, to start
the game you may want to shuffle the deck —place the cards in some random
order. One could use 51 random numbers in the range 1..52 to indicate the order
of the cards. As another example, to roll a pair of dice requires two random num-
bers in the range 1..6 to indicate which sides are face up. More serious applica-
tions also require random numbers. To gain insight into physical models, physi-
cists write simulations of various physical events that require random numbers
—perhaps dealing with weather, or atoms and molecules, or stars.
Random numbers are usually generated on the computer in the following
way. One starts with a first value r 0 (say), called the seed . The first requested
random number r 1 is generated from r 0 using some formula. The second
requested random number r 2 is generated from r 1 using the same formula. The
third one r 3 is generated from r 2 using the same formula. And so on.
So, “random numbers” are not really random. They are generated in an
orderly, regulated fashion from a given seed. If you start with the same seed
again, you get the same sequence of numbers (which is useful when debugging).
Search WWH ::

Custom Search