中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java阿拉伯數字轉中文數字

發布時間:2020-10-16 02:38:32 來源:腳本之家 閱讀:258 作者:張鹿鹿 欄目:編程語言

本文實例為大家分享了java阿拉伯數字轉換成中文數字的具體代碼,供大家參考,具體內容如下

package org.lulu.learn.work;

import java.io.*;

/**
 * Project: Day07
 * Created: Lulu
 * Date: 2016/8/5
 */
public class Work02 {
 public static void main(String[] args) {
//  int num = 0;
//  System.out.println(tranWan(num));

  try(BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("res/data.txt")));
   BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("res/result.txt")))
    ){

   String str = "";
   int percent = 0;

   while ((str = br.readLine()) != null) {
    bw.write(tranWan(Integer.parseInt(str)));
    bw.newLine();
    percent++;

    Thread.sleep(100);

    System.out.print("\r[");
    for (int i = 0; i < 20; i++) {
     if(i < percent/5){
      System.out.print("=");
     }else if(i == percent/5){
      System.out.print(">");
     }else{
      System.out.print(" ");
     }

    }
    System.out.print("]");
    System.out.printf("\t%.2f%%", (float)percent);
   }


  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } catch (InterruptedException e) {
   e.printStackTrace();
  }

 }


 /**
  * 處理千萬數字方法
  *
  * @param num
  * @return
  */
 private static String tranWan(int num) {
  StringBuilder builder = new StringBuilder();
  if (num / 10000 > 0) {//說明
   builder.append(trans(num / 10000)).append("萬");
  }
  int temp = num % 10000;
  if (temp > 0) {
   String trans = trans(temp);
   //首先判斷是否有萬位,
   if (builder.length() > 0) {

    //如果千位為0, 則需要補零
    if (temp / 1000 == 0) {
     builder.append("零");
    }
   }
   builder.append(trans);

  }
  if (builder.length() == 0) {
   builder.append("零");
  }
  return builder.toString();
 }

 /**
  * 完成4位數轉換
  *
  * @param num
  * @return
  */
 private static String trans(int num) {
  String[] numeric = new String[]{"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
  StringBuilder builder = new StringBuilder();
  builder.append(numeric[num / 1000] + "千").
    append(numeric[num / 100 % 10] + "百").
    append(numeric[num / 10 % 10] + "十").
    append(numeric[num % 10]);
  //去掉了零千....
  int index = -1;
  while ((index = builder.indexOf(numeric[0], index + 1)) != -1) {
   if (index < builder.length() - 1) {
    builder.deleteCharAt(index + 1);
   }
  }
  //去掉雙零
  index = 0;
  while ((index = builder.indexOf("零零", index)) != -1) {
   builder.deleteCharAt(index);
  }

  if (builder.length() > 1) {
   //去掉開頭的零
   if (builder.indexOf(numeric[0]) == 0) {
    builder.deleteCharAt(0);
   }
   //去掉末尾的零
   if (builder.indexOf(numeric[0]) == builder.length() - 1) {
    builder.deleteCharAt(builder.length() - 1);
   }

  }
  //把開頭一十換成十
  if (builder.indexOf("一十") == 0) {
   builder.deleteCharAt(0);
  }
  return builder.toString();
 }
}

再為大家分享一段:java實現阿拉伯數字轉換為漢字數字

private static String toChinese(String str) {
  String[] s1 = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
  String[] s2 = { "十", "百", "千", "萬", "十", "百", "千", "億", "十", "百", "千" };
  String result = "";
  int n = string.length();
  for (int i = 0; i < n; i++) {
   int num = string.charAt(i) - '0';
   if (i != n - 1 && num != 0) {
    result += s1[num] + s2[n - 2 - i];
   } else {
    result += s1[num];
   }
   System.out.println(" "+result);
  }
  System.out.println(result);
  return result;
 }

 public static void main(String[] args) {
  Scanner scanner = new Scanner(System.in);
  System.out.println("輸入字符串:");
  String str = scanner.next();
  // 將字符串數字轉化為漢字
  toChinese(str);
 } 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

新宁县| 花莲市| 隆林| 金坛市| 武乡县| 龙口市| 无锡市| 通州市| 义马市| 富蕴县| 楚雄市| 长武县| 六盘水市| 东乡| 临澧县| 苏尼特左旗| 双辽市| 江油市| 新兴县| 铜陵市| 永和县| 渝北区| 阜新| 东乌珠穆沁旗| 金沙县| 玛曲县| 清水河县| 金寨县| 灵寿县| 台州市| 乌兰察布市| 广德县| 滦南县| 监利县| 正蓝旗| 红原县| 锡林浩特市| 达日县| 黑龙江省| 应用必备| 黔江区|