개발자의 끄적끄적

[java] 소수점 고정시키기 본문

개발/java & jsp

[java] 소수점 고정시키기

효벨 2020. 3. 2. 22:19
728x90
반응형

[java] 소수점 고정시키기

 

import java.text.DecimalFormat;

public class DecimalFormatTest {

    public static void main(String[] args)  {

        DecimalFormat df = new DecimalFormat("#######.00");

        DecimalFormat df2 = new DecimalFormat("#######.000");  

          

        String number1 = df.format(55);

        String number2 = df.format(42.196);

        String number3 = df.format(2.1);

          

        String number4 = df2.format(55);

        String number5 = df2.format(42.196);

        String number6 = df2.format(2.1);

   

        System.out.println("number1 55  ==> " + number1);

        System.out.println("number2 42.196  ==> " + number2);

        System.out.println("number3 2.1  ==> " + number3);

          

        System.out.println("number4 55  ==> " + number4);

        System.out.println("number5 42.196  ==> " + number5);

        System.out.println("number6 2.1  ==> " + number6);      

          

    }

}

 

결과값

 

number1 55  ==> 55.00

number2 42.196  ==> 42.20

number3 2.1  ==> 2.10

 

number4 55  ==> 55.000

number5 42.196  ==> 42.196

number6 2.1  ==> 2.100

 

 

출처 : http://chongmoa.com/java/8435

반응형
Comments