export const numberToChinese = (num:number) => {
    const chineseNums = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
    const unit = ['', '十', '百', '千'];
    const unitSection = ['', '万', '亿'];
  
    let result = '';
    let section = 0;
  
    while (num > 0) {
      let sectionNum = num % 10000;
      let sectionStr = '';
  
      let ten = false;
      for (let i = 0; i < 4; i++) {
        let digit = sectionNum % 10;
        if (digit !== 0) {
          if (i > 0 && ten) {
            sectionStr = chineseNums[digit] + unit[i] + sectionStr;
          } else {
            sectionStr = chineseNums[digit] + unit[i] + sectionStr;
          }
          ten = digit === 1 && i === 1;
        } else if (i > 0 && sectionStr.length > 0) {
           if (sectionStr[0] !== '零' && !['十', '百', '千'].includes(sectionStr[0]) ){
               sectionStr = '零' + sectionStr;
           }
           if (sectionStr.startsWith("零零")){
               sectionStr = sectionStr.slice(1);
           }
  
          ten = false;
  
        }
  
        sectionNum = Math.floor(sectionNum / 10);
      }
  
      if (sectionStr.endsWith("零")) {
         sectionStr = sectionStr.slice(0,-1);
      }
  
      if (sectionStr.length > 0) {
          result = sectionStr + unitSection[section] + result;
  
      }
  
      num = Math.floor(num / 10000);
      section++;
    }
  
    result = result.replace(/零+/g, '零');
    if (result.endsWith('零')) {
        result = result.slice(0,-1);
    }
    if (result.startsWith('一十')) {
        result = result.slice(1);
    }
      if (result.startsWith('零')) {
        result = result.slice(1);
    }
     if(result === ''){
          return '零'
      }
  
    return result;
  }