MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

/*
0.0 formats as 000.00
1.0 formats as 001.00
3.142857142857143 formats as 003.1429
100.2345678 formats as 100.2346
 */
import java.text.NumberFormat;

public class MainClass {

    public static void main(String[] av) {
        double data[] = { 0, 1, 22d / 7, 100.2345678 };
        NumberFormat form = NumberFormat.getInstance();

        // Set it to look like 999.99[99]
        form.setMinimumIntegerDigits(3);
        form.setMinimumFractionDigits(2);
        form.setMaximumFractionDigits(4);

        // Now print using it.
        for (int i = 0; i < data.length; i++)
            System.out.println(data[i] + "\tformats as " + form.format(data[i]));
    }

}