Sunday, August 2, 2009

How to set significant figures in Java Programming language?

I'm writing a simple ATM machine for my java programming class. I currently have it all set to work and my only problem is that when there is no change to be displayed. EX 100.00, is displayed as 100.0, or One dollar is displayed as 1.0. How can I set it to display currectly like a true ATM machine would do with both decimal places. Thanks for you help

How to set significant figures in Java Programming language?
Java 1.5 offers a printf-like formatting via java.util.Formatter or via the 'printf' method of an output stream:





e.g.: System.out.printf("%.2f", (double)100); or


String output = String.format("%.2f", (double)100);
Reply:The answer you are looking for is the printing. You select how many decimals there are at the print stage. Look at the DecimalFormat class for the secrets.





However this is NEVER done in the real world. You'd use something like BCD or integer arithmetic to ensure that you have no awkward decimal fractions to deal with.

sd cards

No comments:

Post a Comment