One of the questions that I'm regularly asked on Java courses is how to format numbers to a certain number of decimal places - Java lacks the printf function of languages like Perl and C, and it lacks Python's % operator too.
You can use a DecimalFormat object.
You create such an object for (each) different format that you wish to use, and then apply it as often as you like to values that you wish to format in a particular way. It has the great advantage over "printf" type schemes in that you only specify the format once, and then apply it as many times as you like - thus if you want to change the format you're using there's only a single edit to make and not a whole raft of edits.
Here's a code example - generously distributing a million pounds equally between a number of people:
import java.text.*;
public class Generous {
public static void main (String [] args) {
double giveaway = 1000000.0;
DecimalFormat Currency = new DecimalFormat("#0.00 pounds");
for (int rec = 30; rec <= 45; rec++) {
double amount = giveaway / rec;
System.out.print ("With "+rec+ " recipients ");
String howmuch = Currency.format(amount);
System.out.println ("each gets "+howmuch);
}
}
}
Here are the results:
[trainee@thursday gj]$ java Generous
With 30 recipients each gets 33333.33 pounds
With 31 recipients each gets 32258.06 pounds
With 32 recipients each gets 31250.00 pounds
With 33 recipients each gets 30303.03 pounds
With 34 recipients each gets 29411.76 pounds
With 35 recipients each gets 28571.43 pounds
With 36 recipients each gets 27777.78 pounds
With 37 recipients each gets 27027.03 pounds
With 38 recipients each gets 26315.79 pounds
With 39 recipients each gets 25641.03 pounds
With 40 recipients each gets 25000.00 pounds
With 41 recipients each gets 24390.24 pounds
With 42 recipients each gets 23809.52 pounds
With 43 recipients each gets 23255.81 pounds
With 44 recipients each gets 22727.27 pounds
With 45 recipients each gets 22222.22 pounds
[trainee@thursday gj]$
In a longer program, you would typically encapsulate the DecimalFormat object within a class, so that every time you printed out an Object of a certain type, it would be applied. You'll commonly find it used within toString methods ...
Using this model, here's the application that doesn't have to bother with how the number is formatted:
public class Gen2 {
public static void main (String [] args) {
double giveaway = 1000000.0;
for (int rec = 40; rec <= 45; rec++) {
Gift amount = new Gift(giveaway / rec);
System.out.print ("With "+rec+ " recipients each gets ");
System.out.println (amount);
}
}
}
and here's the definition of the Gift object:
import java.text.*;
public class Gift {
double money;
public Gift (double money) {
this.money = money;
}
public String toString() {
DecimalFormat Currency = new DecimalFormat("#0.00 pounds");
return Currency.format(money);
}
}
See also
Input and Output in Java
Please note that articles in this section of our
web site were current and correct to the best of our ability when published,
but by the nature of our business may go out of date quite quickly. The
quoting of a price, contract term or any other information in this area of
our website is NOT an offer to supply now on those terms - please check
back via
our main web site
Java - More Input and Output [1239] - ()
[2420] - ()
[4350] - ()
[4414] - ()
[4420] - ()
resource index - Java
Solutions centre home page
You'll find shorter technical items at
The Horse's Mouth and
delegate's questions answered at
the
Opentalk forum.
At Well House Consultants, we provide
training courses on
subjects such as Ruby, Lua, Perl, Python, Linux, C, C++,
Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer)
many questions, and answers to those which are of general
interest are published in this area of our site.