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.

Recommended Answers

All 2 Replies

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));
   }
   
}

Wow thanks that worked really well.

Thanks very much, appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.