码迷,mamicode.com
首页 > 其他好文 > 详细

13、数字格式化和数字分组

时间:2014-12-17 17:48:09      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:style   blog   ar   color   使用   sp   for   java   div   

package com.lei.duixiang;

import java.text.DecimalFormat;



public class DecimalFormatSimpleDemo {

    /**
     * 1、数字格式化
     * 2、数字分组
     * 更多请参考 DecimalFormat类中特殊字符说明
     * @param args
     */

    //使用实例化对象时设置格式化模式
    static public void SimpleFormat(String pattern,double value){
        System.out.println("---------SimpleFormat-------------");
        //实例化 DecimalFotmat 对象
        DecimalFormat myFormat = new DecimalFormat(pattern);
        String output = myFormat.format(value);    //将数字格式化
        System.out.println(value + " "+pattern + " "+output);
    } 

    //使用 applyPattern() 方法对数字进行格式化
    static public void UseApplyPatternMethodFormat(String pattern,double value){
        System.out.println("---------applyPattern-------------");
        //实例化 DecimalFotmat 对象
        DecimalFormat myFormat = new DecimalFormat(pattern);
        myFormat.applyPattern(pattern);    //调用 applyPattern() 方法设置格式化模板
        System.out.println(value + " "+pattern + " "+myFormat.format(value));
    }
    
    //数字分组
    public static void DecimalMethod(){
        DecimalFormat myFormat = new DecimalFormat();
        myFormat.setGroupingSize(2);    //设置将数字分组为 2 
        String output = myFormat.format(123456.789);
        System.out.println("将数字以每两个数字分组:"+output);
        myFormat.setGroupingUsed(false);    //设置不允许数字进行分组
        String output2 = myFormat.format(123456.789);
        System.out.println("不允许数字分组" + output2);
    }

    public static void main(String[] args) {
        SimpleFormat("###, ###.###",123456.789);    //调用静态 SimpleFormat 方法
        SimpleFormat("000000000.###kg", 123456.789); //在数字后加上单位
        //格式化模板格式化数字,不存在的位以 0 显示
        SimpleFormat("000000.000", 123.78);
        //调用静态 UseApplyPatternMethodFormat() 方法
        UseApplyPatternMethodFormat("#.###%", 0.789);    //将数字后加上单位
        //将小数点后格式化为 两位
        UseApplyPatternMethodFormat("###.##", 123456.789);
        //将数字转换为千分数形式
        UseApplyPatternMethodFormat("0.00\u2030", 0.789);
        
        //调用数字分组方法
        System.out.println("--------数字分组方法--------");
        DecimalMethod();
    }

}

 

13、数字格式化和数字分组

标签:style   blog   ar   color   使用   sp   for   java   div   

原文地址:http://www.cnblogs.com/spadd/p/4169795.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!