Feb 14, 2012

[Java] setText format (how to round number)

// Java
// how to round number


import java.text.DecimalFormat;

class round_number
{

 public static void main(String[] args)    // main method
 {
        
        DecimalFormat df = new DecimalFormat("#.##");
        
        double num = 3.141592;

        System.out.println(df.format(num));

 }
}

-----------------------------------------------------------------
in GUI (Netbeans),
import java.text.DecimalFormat;   // outside class

//inside method
DecimalFormat df = new DecimalFormat("#.##");
jLabel1.setText(df.format(num));

No comments:

Post a Comment