故乡春暖花开
好好生活
//在js中,将数值3位1逗号显示
function toMoney(num) {
  if (num==0) {
    return 0;
  }
  var split = (num + "").split('.'),
      result="";  
  result += split[0].replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
  if (split[1]) {
      result += ("." +  split[1]);
  }
  return  result;
 }
//在java中 数据格式化显示 
/**
     * 判断字符串是否为数值
     * @param str
     * @return
     */
    public static boolean isNum(String str){
    return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
  }
  
  /**
   * 字符串整数化,格式为3位1逗,eg 23345-->23,345
   * @param value
   * @return
   */
public String setInt(String value) {
  if (!isNum(value)||value.indexOf(".")>-1) {
    return value;
  }
  double d = Double.parseDouble(value); eg:12345.678
  DecimalFormat format = new DecimalFormat();
  String style="#,##0";     //12,346 --小数部分被切,返回整数
  //  style="#,##0.#";      //12,345.7 ---1位小数
  //  style="#,##0.###0";   //12,345.6780 ---4位小数,不够4位补0
  //  style="#,##0.####";   //12,345.678 ---4位小数,不够不补0
  //  style="##.##%";       //1234567.8%
  //  style="00.00%";       //1234567.80%

  format.applyPattern(style);
  format.setRoundingMode(HALF_UP); 
  String content = format.format(d);
  return content;
}

 

 

posted on 2016-01-18 09:21  Linatao  阅读(171)  评论(0)    收藏  举报