您现在的位置是:首页 > 文章详情

Java高级之数据格式化

日期:2018-05-04点击:544
Labmda表达式:

Runnable runnable =new Runnable(){

    public void run(){

        //123    

    }

}

变:

Runnable runnable =()->{

        //123    

}

“123”为多句代码,可以写上括号,如果仅一句,可以不写。


获得小数位数:

经测试,使用小数(1.01)通过使用“.”进行split操作后,得到的数组为空,原因在于String的split方法

 public String[] split(String regex, int limit) { /* fastpath if the regex is a (1)one-char String and this character is not one of the RegEx's meta characters ".$|()[{^?*+\\", or (2)two-char String and the first char is the backslash and the second is not the ascii digit or ascii letter. */ char ch = 0; if (((regex.value.length == 1 && ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) || (regex.length() == 2 && regex.charAt(0) == '\\' && (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 && ((ch-'a')|('z'-ch)) < 0 && ((ch-'A')|('Z'-ch)) < 0)) && (ch < Character.MIN_HIGH_SURROGATE || ch > Character.MAX_LOW_SURROGATE)) { ... } return Pattern.compile(regex).split(this, limit); }

if语句判断无法通过,因为不允许使用"."做split的regex字符;第2个条件明显长度只有1;第3个条件ch<55276或者ch>57344才满足。

运行到return语句,Pattern执行结果,获得空数据。

那么只能通过indexOf方法来获得小数位数了:

1位小数且小数第1位是0的(如1.0),显示整数;其他情况显示原小数(String默认会把1.10看成1.1)。

 public static String get1BitData(double number) { String first = String.valueOf(number); String second = first.substring(first.indexOf(".")); if (second.length() > 1 && !second.endsWith("0")) {     return String.valueOf(number); } return String.valueOf((int) number); }


取相应格式的小数:

  1.         // 取一位整数  
  2.         System.out.println(new DecimalFormat("0").format(pi));// 3  
  3.         // 取一位整数和两位小数  
  4.         System.out.println(new DecimalFormat("0.00").format(pi));// 3.14  
  5.         // 取两位整数和三位小数,整数不足部分以0填补。  
  6.         System.out.println(new DecimalFormat("00.000").format(pi));// 03.142  
  7.         // 取所有整数部分  
  8.         System.out.println(new DecimalFormat("#").format(pi));// 3  
  9.         // 以百分比方式计数,并取两位小数  
  10.         System.out.println(new DecimalFormat("#.##%").format(pi));// 314.16%  
  11.   
  12.         long c = 299792458;  
  13.         // 显示为科学计数法,并取五位小数  
  14.         System.out.println(new DecimalFormat("#.#####E0").format(c));// 2.99792E8  
  15.         // 显示为两位整数的科学计数法,并取四位小数  
  16.         System.out.println(new DecimalFormat("00.####E0").format(c));// 29.9792E7  
  17.         // 每三位以逗号进行分隔。  
  18.         System.out.println(new DecimalFormat(",###").format(c));// 299,792,458  
  19.         // 将格式嵌入文本  
  20.         System.out.println(new DecimalFormat("光速大小为每秒,###米。").format(c));  


Ioc反转和Di注入的可用容器有Spring、JBoss、Ejb等。

它们都是一种编程思想,目的是解耦。

A调用B,正常情况下是,创建B,再调用B,则A依赖B,并且使用完毕还要销毁B。

Ioc,A告诉容器要调用B,容器创建B,并通知A,然后A通过构造函数、属性和接口调用方式,获得B,再去调用B。

Di,A告诉容器要调用B,容器创建B,并通知A,然后A通过反射的方式,获得B,再去调用B。

AOP:Aspect Oriented Programming。面向切面编译,切面指某类的某方法的代码片断,切点(JoinPoint)指某类的某方法。AOP指通过动态注入的方式,将几处共用的代码片断,在使用时注入,使得代码维护更加便利。

原文链接:https://yq.aliyun.com/articles/595905
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章