免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1188 | 回复: 0
打印 上一主题 下一主题

java数据值的格式化 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-06-10 13:35 |只看该作者 |倒序浏览
通过使用java.text package包中提供的类型,将数字类型装换成指定的格式。
http://www.javaalmanac.com/egs/index.html
// The 0 symbol shows a digit or 0 if no digit present
    NumberFormat formatter = new DecimalFormat("000000");
    String s = formatter.format([color="#0066ff"]-1234.567);  // -001235
    // notice that the number was rounded up
   
    // The # symbol shows a digit or nothing if no digit present
    formatter = new DecimalFormat("##");
    s = formatter.format([color="#0066ff"]-1234.567);         // -1235
    s = formatter.format([color="#0066ff"]0);                 // 0
    formatter = new DecimalFormat("##00");
    s = formatter.format([color="#0066ff"]0);                 // 00
   
   
    // The . symbol indicates the decimal point
    formatter = new DecimalFormat(".00");
    s = formatter.format([color="#0066ff"]-.567);             // -.57
    formatter = new DecimalFormat("0.00");
    s = formatter.format([color="#0066ff"]-.567);             // -0.57
    formatter = new DecimalFormat("#.#");
    s = formatter.format([color="#0066ff"]-1234.567);         // -1234.6
    formatter = new DecimalFormat("#.######");
    s = formatter.format([color="#0066ff"]-1234.567);         // -1234.567
    formatter = new DecimalFormat(".######");
    s = formatter.format([color="#0066ff"]-1234.567);         // -1234.567
    formatter = new DecimalFormat("#.000000");
    s = formatter.format([color="#0066ff"]-1234.567);         // -1234.567000
   
   
    // The , symbol is used to group numbers
    formatter = new DecimalFormat("#,###,###");
    s = formatter.format([color="#0066ff"]-1234.567);         // -1,235
    s = formatter.format([color="#0066ff"]-1234567.890);      // -1,234,568
   
    // The ; symbol is used to specify an alternate pattern for negative values
    formatter = new DecimalFormat("#;(#)");
    s = formatter.format([color="#0066ff"]-1234.567);         // (1235)
   
    // The ' symbol is used to quote literal symbols
    formatter = new DecimalFormat("'#'#");
    s = formatter.format([color="#0066ff"]-1234.567);         // -#1235
    formatter = new DecimalFormat("'abc'#");
    s = formatter.format([color="#0066ff"]-1234.567);         // -abc1235


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/1507/showart_30249.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP