Wednesday, April 18, 2012

How to set DecimalFormat in JAVA?

   Paano ba mag set ng decimal format tulad kapag ganito ang program decimals ? Diba ang dami figures? Para mai-set ng ayos depende sa gusto mo, eto basic program para mas madali maintindihan how it works. Use DeciFormat.

// first, import DecimalFormat 
import java.text.DecimalFormat;


public class decimalFormat {
    public static void main(String[] args) {

     //set variable "point" sa gusto mo na format
     DecimalFormat point=new DecimalFormat ("0.00");

     //set value 'a' at 'b'
    //declare as double, kasi pag int type ang variable walang decimal points ang output
     double a=10,b=3;
     double quotient=a/b;

    //print output
     System.out.println("the quotient is "+ point.format(quotient));
    }
}Done!

Take note "point.format(quotient)" ,ganito ang pag gamit ng format na gusto mo. Kung basic na output code at hindi mo gagamitan ng decimal format ganito ang magiging output nyan.

output:
the quotient is 3.3333333333333335


SHARE and LEARN!

No comments:

Post a Comment