MDA/MDD/TDD/DDD/DDDDDDD
          posts - 536, comments - 111, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          java數(shù)字、字符串處理

          Posted on 2009-04-27 00:02 leekiang 閱讀(410) 評(píng)論(0)  編輯  收藏 所屬分類: java
          1,從excel中取得的數(shù)字要么帶".0",要么是1.33E8之類,用NumberFormat搞定
          ?????? String value = "";
          ??? ??? if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
          ??? ??? ??? double d = cell.getNumericCellValue();
          ??? ??? ??? NumberFormat formatter = NumberFormat.getNumberInstance();
          ??? ??? ??? formatter.setGroupingUsed(false);
          ??? ??? ??? value = formatter.format(d);

          ??? ??? } else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
          ??? ??? ??? value = cell.getStringCellValue();
          ??? ??? }
          ??? ??
          2,???? String s="3,4,5,6,";
          ??? ??? System.out.println(s.split(",").length);
          ????? 輸出4,我記得以前的輸出好像是5?


          3, 以正常的方式顯示double數(shù)字的值,好像比較困難。
          public String double2String(double d, int fNumber) {
          ??? ??? if (fNumber < 0)
          ??? ??? ??? fNumber = 0;

          ??? ??? String pattern = null;
          ??? ??? switch (fNumber) {
          ??? ??? case 0:
          ??? ??? ??? pattern = "#0"; //$NON-NLS-1$
          ??? ??? ??? break;
          ??? ??? default:
          ??? ??? ??? pattern = "#0."; //$NON-NLS-1$
          ??? ??? ??? StringBuffer b = new StringBuffer(pattern);
          ??? ??? ??? for (int i = 0; i < fNumber; i++) {
          ??? ??? ??? ??? b.append('#');
          ??? ??? ??? }
          ??? ??? ??? pattern = b.toString();
          ??? ??? ??? break;

          ??? ??? }
          ??? ??? DecimalFormat formatter = new DecimalFormat();
          ??? ??? formatter.applyPattern(pattern);
          ??? ??? String value = formatter.format(d);
          ??? ??? return value;
          ??? }


          public String SicenToComm(double value) {?
          ???????? String retValue = null;?
          ???????? NumberFormat fmt= NumberFormat.getNumberInstance();
          ???????? //DecimalFormat fmt= new DecimalFormat();? //效果一樣
          ???????? fmt.setMinimumFractionDigits(5);? //
          ???????? fmt.setMaximumFractionDigits(5);? //什么意思
          ???????? retValue = fmt.format(value);?
          ???????? System.out.println(retValue);?
          ???????? retValue = retValue.replaceAll(",","");? //去掉逗號(hào)
          ???????? return retValue;?
          ???? }
          以上兩種方式都不能全部顯示543854839493943.4533656這樣的數(shù)
          主站蜘蛛池模板: 石泉县| 平乡县| 扶沟县| 怀来县| 长海县| 额敏县| 达拉特旗| 二连浩特市| 吉木萨尔县| 翁牛特旗| 华阴市| 明光市| 绥江县| 叶城县| 阜阳市| 岳西县| 洛扎县| 临泽县| 宝应县| 湖州市| 双牌县| 义马市| 临邑县| 七台河市| 舒城县| 华安县| 科技| 榕江县| 衡山县| 南平市| 甘孜| 色达县| 星子县| 涟水县| 太保市| 上思县| 张北县| 祁门县| 华安县| 始兴县| 巴青县|