转-数字格式化

来源:互联网 发布:mac qq游戏 编辑:程序博客网 时间:2024/05/22 12:30
  Java数字、货币值和百分数等的格式化处理(转载) 06.07.19  from david.zhang 相关文章(1) 来源 以文找文 上一篇    下一篇   这篇文章不错,我也要收藏    

Java数字、货币值和百分数等的格式化处理                                          

如果我们用下列语句输出一个数

System.out.println(123456.789);

将会在Console看到输出

123456.789

那么如何得到123,456.789这种格式化的输出呢?这里就需要用到java.text.Format这个类。不仅是数字,它还提供了货币值和百分数的格式化输出,比如0.58的百分数输出形式是58%。要获得本地的默认格式,可以用下列方法获得

NumberFormat.getNumberInstance()


NumberFormat.getCurrencyInstance()


NumberFormat.getOPercentInstance()

而要获得某个国家或地区的具体格式,可以使用参数Local.XXX,Locale.GERMANYLocale.UK

NumberFormat formatter = NumberFormat.getNumberInstance(Locale.GERMANY);

范例

 

import java.text.NumberFormat;
import java.util.Locale;

public class FormatTest {

    public static void main(String args[]) {

        // 不使用格式化输出数
        double d = 10000.0 / 3.0;
        System.out.println("无格式化输出:" + d);

        // 使用本地默认格式输出数
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        //numberFormat.setMaximumFractionDigits(4);
        //numberFormat.setMinimumIntegerDigits(6);
        String numberString = numberFormat.format(d);
        System.out.println("本地默认格式输出数:" + numberString);

        // 使用本地默认格式输出货币值
        NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
        System.out.println("本地默认格式输出货币值:" + currencyFormat.format(d));

        // 使用本地默认格式输出百分数
        NumberFormat percentFormat = NumberFormat.getPercentInstance();
        System.out.println("本地默认格式输出百分数:" + percentFormat.format(d));

        // 在不同的国家和地区数字表示的格式也有区别。如德国
        // 使用德国的格式化输出数
        NumberFormat numberFormatG = NumberFormat
                .getNumberInstance(Locale.GERMANY);
        System.out.println("德国数字输出形式:" + numberFormatG.format(d));

        // 使用德国货币输出形式
        NumberFormat currencyFormatG = NumberFormat
                .getCurrencyInstance(Locale.GERMANY);
        System.out.println("德国货币输出形式:" + currencyFormatG.format(d));

        // 使用美国货币输出形式
        NumberFormat currencyFormatA = NumberFormat
                .getCurrencyInstance(Locale.US);
        System.out.println("美国货币输出形式:" + currencyFormatA.format(d));

        // 使用德国百分数输出形式
        NumberFormat percentFormatG = NumberFormat
                .getPercentInstance(Locale.GERMANY);
        System.out.println("德国百分数输出形式:" + percentFormatG.format(d));
        
        System.exit(0);
    }
}

 


程序输出

<!--[if !vml]--><!--[endif]-->

由于欧元符号无法在此Console输出,所以显示?

可以指定显示的最多(或最少)整数位和小数位。如

double d = 10000.0 / 3.0;


NumberFormat numberFormat = NumberFormat.getNumberInstance();


numberFormat.setMaximumFractionDigits(4);


numberFormat.setMinimumIntegerDigits(6);


String numberString = numberFormat.format(d);


System.out.println(numberString);

输出:
003,333.3333。整数位不够的补零,小数截去部分四舍五入。

    也可以利用NumberFormat的一个子类DecimalFormat来指定输出格式。

DecimalFormat decimalFormat = new DecimalFormat("######.0000");


String s = decimalFormat.format(d);


和前面一样,显示六个整数位和4个小数位。 

下面对格式化的数字进行解析。

import java.util.Locale;
import java.text.NumberFormat;
import java.text.ParseException;

public class ParseFormat {

    public static void main(String args[]) {

        // 本地格式的解析
        NumberFormat numberFormat1 = NumberFormat.getNumberInstance();
        Number numb1 = null;
        try {
            numb1 = numberFormat1.parse("33,333.33");
        } catch (ParseException e1) {
            System.err.println(e1);
        }
        System.out.println(numb1);
       
       
        // 以德国格式解析
        NumberFormat numberFormat2 = NumberFormat
                .getNumberInstance(Locale.GERMAN);
        Number numb2 = null;
        try {
            numb2 = numberFormat2.parse("33,333.33");
        } catch (ParseException e2) {
            System.err.println(e2);
        }
        System.out.println(numb2);
       
        System.exit(0);
    }
}

程序输出:
33333.33
33.333

    同样一种格式33,333.33,有人将之理解为33333.33,也有人认为它是33.333,软件国际化的重要性可见一斑。

  <script type="text/javascript"><!--google_ad_client = "pub-6625678643128649";google_alternate_color = "FFFFFF";google_ad_width = 728;google_ad_height = 90;google_ad_format = "728x90_as";google_ad_type = "text_image";google_ad_channel ="";//--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script><iframe width="728" scrolling="no" height="90" frameborder="0" allowtransparency="true" hspace="0" vspace="0" marginheight="0" marginwidth="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-6625678643128649&amp;dt=1199328815446&amp;lmt=1199328815&amp;alt_color=FFFFFF&amp;format=728x90_as&amp;output=html&amp;correlator=1199328815446&amp;url=http%3A%2F%2Fwww.360doc.com%2FshowWeb%2F0%2F0%2F159679.aspx&amp;ad_type=text_image&amp;ref=http%3A%2F%2Fwww.google.cn%2Fsearch%3Fcomplete%3D1%26hl%3Dzh-CN%26newwindow%3D1%26sa%3DX%26oi%3Dspell%26resnum%3D0%26ct%3Dresult%26cd%3D1%26q%3D%25E6%2595%25B0%25E5%25AD%2597%25E6%25A0%25BC%25E5%25BC%258F%25E5%258C%2596%2Bjava%26spell%3D1&amp;cc=27&amp;ga_vid=1911478582.1199328815&amp;ga_sid=1199328815&amp;ga_hid=34314796&amp;flash=9&amp;u_h=1024&amp;u_w=1280&amp;u_ah=994&amp;u_aw=1280&amp;u_cd=32&amp;u_tz=480&amp;u_his=1&amp;u_java=true&amp;u_nplug=12&amp;u_nmime=47" name="google_ads_frame"></iframe>  

 
     
 
 
原创粉丝点击