Dart数字转汉字

2022-06-17  本文已影响0人  吃饱了就送
class ConvertNumberToChineseMoneyWords{
  // 大写数字
  static List<String> NUMBERS = ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十",""];

  static String toChinese(int index) {
    if(index > 100){
      return index.toString();
    }
    StringBuffer stringBuffer = StringBuffer();
    if(index / 10 < 1){
      return NUMBERS[index];
    }
    int tenUnit = (index / 10).toInt();
    int remainder = index % 10;
    if(remainder == 9){
      tenUnit++;
      remainder = 10;
    }
    if(tenUnit == 1){
      stringBuffer..write("十")..write(NUMBERS[remainder]);
      return stringBuffer.toString();
    }
    stringBuffer..write(NUMBERS[tenUnit - 1])..write("十")..write(NUMBERS[remainder]);
    return stringBuffer.toString();
  }
}
上一篇下一篇

猜你喜欢

热点阅读