0

Hi,

In a terminal program I'm writing values are inputted and stored in a pair of arrays. At one point in the program I need to print them back out.

My problem is that if I input 1.10, Java gives me 1.1. I really need it to print the zero even if its in the least significant place.

Searching around on Google suggested using DecimalFormat, but I can't get that working. Any ideas?

Thanks,
Bob.

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Moonrise_state
1

NumberFormat is an abstract class; use its static factory method to get your choice of number formatter.

public class MainTest {
   
   public static void main(String args[]) throws Exception {
      // Get a number formatter for the default locale
      NumberFormat nf = NumberFormat.getNumberInstance();
      nf.setMinimumFractionDigits(2);
      System.out.println(nf.format(1.1));
      
      // Get a number formatter for a different locale e.g. french
      nf = NumberFormat.getNumberInstance(Locale.FRENCH);
      nf.setMinimumFractionDigits(2);
      System.out.println(nf.format(1.1));
   }
   
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.