• 202阅读
  • 3回复

[提问]在QT中如何实现数字大小写的转换的输出? [复制链接]

上一主题 下一主题
离线谡左觞唐
 

只看楼主 倒序阅读 楼主  发表于: 06-11
回复本帖可获得2点威望奖励!
每人最多可获奖1次,奖池剩余4点威望 (中奖几率10%)
感觉数字大小写转换的代码应该是没有问题的,但是死活折腾不出输出的部分求助诸位大佬帮忙看下怎么改啊?

以下是代码:
void MainWindow::on_pushButton_clicked()
{
    QString Left_of_Point=number_Transfer_BigChinese(&number);
    QString number;    
    QString numberSign;
    //QString qstr=QString::fromStdString(Left_of_Point);  
//QStringList  numerical_unit   =   {"","拾","佰","仟","万","拾", "佰", "仟", "亿", "拾", "佰", "仟"};
//单位表  
//QStringList  numerical_value  = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};//数字表    
//ui->textBrowser->clear();
    //ui->textBrowser->setText(qstr);
         //QString str1=ui->lineEdit->text();        
//QString numerical_value=str1.toStdString();      
   //double number=atof(numerical_value.c_str());      
   //QString numerical_unit=number_Transfer_BigChinese(number);      
   //QString qstr=QString::fromStdString(numerical_unit);        
ui->textBrowser->setText();
}
const QString number_Transfer_BigChinese(const double &Fnumber)
{
    if(qAbs(Fnumber)<0.01)   //保留2位小数时,近似看作零的情况        
return "零";

    //判断正负号  
QString numberSign;
//存储符号  
if(Fnumber<0)        numberSign = "(负数)";   //将数据的绝对值 转换成字符串,如-58 转成 “58.00”   QString number = QString::number(qAbs(Fnumber),'f',2);//qAbs绝对值 ,保留两位小数   QString Left_of_Point;//整数部分   int length =number.length()-3;//整数部分的长度,(精确度为2,去除小数和小数点)   if(length>12)   {        //qDebug()<<"输入的数值超过范围!"        return "输入的数值超过范围!";   }
   QStringList numerical_unit   =   {"","拾","佰","仟","万","拾", "佰", "仟", "亿", "拾", "佰", "仟"};  
QStringList numerical_value  =   {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
   //数数整数低位多少个连零   int counter_0=0;   for(int i =length-1;i>=0;i--)   {       if((number.toLatin1()-'0' )==0) //从个位开始,向高位走           counter_0++;  
    else    
      break;
   }  
if(length==1 && counter_0==1) //0.x    
   counter_0=0; //不进行过滤
   //1400  0.2
   for(int i=0,flag=1;i<length-counter_0;i++)  
{        //5     8       1    2   3        //伍        壹佰 贰拾     
  if((number.toLatin1()-'0')==0)      
{           if((flag!=0 && (length-1-i)%4 != 0) || length ==1) //flag!=0  表示前一个数值 不为0
          Left_of_Point+="零"; //后面不用添加 单位  
        if((length-1-i)%4 == 0) //如果0处于分段处,后面需添加单位        
  Left_of_Point+=numerical_unit[length-1-i];//添加数值单位
           flag =0; //标记
       }       else      
{      
flag =1;        
Left_of_Point+=numerical_value[number.toLatin1()-'0']; //'5'-'0'==5    
   Left_of_Point+=numerical_unit[length-1-i];//添加数值单位
       }
  }  
  //QString Right_of_Point;//小数点右侧,小数部分(保留两位)  xxxx.yy
    int totalLength = number.length();
    if(number[totalLength-2]=='0'&&number[totalLength-1]=='0')    
{  
     QString Bigcn=numberSign+Left_of_Point+"吨";  
      return Bigcn;  
}    
else if(number[totalLength-2]!='0'&&number[totalLength-1]=='0')  
{      
  QString Bigcn=numberSign+Left_of_Point+"点"+numerical_value[number[totalLength-2].toLatin1()-'0']+"吨";        
return Bigcn;  
}    
else
    {  
      QString Bigcn=numberSign+Left_of_Point+"点"+numerical_value[number[totalLength-2].toLatin1()-'0']+numerical_value[number[totalLength-1].toLatin1()-'0']+"吨";    
   return Bigcn;  
  }

    return "异常情况";}



离线dbzhang800

只看该作者 1楼 发表于: 06-11
发贴时用代码模式,或者将你的代码封在 代码 里
离线谡左觞唐

只看该作者 2楼 发表于: 06-11
回 dbzhang800 的帖子
dbzhang800:发贴时用代码模式,或者将你的代码封在 代码 里 (2018-06-11 19:19) 

额,不太懂,发出来的就是全部的代码了啊。
离线XChinux

只看该作者 3楼 发表于: 06-12
下面是https://github.com/qtcn/tianchi/blob/master/src/core/tcmath.cpp的代码,供参考,当然这个是将数字转换成大写金额描述的。
  1. QString TcMath::currencyText(const QString &number)
  2. {
  3.     //static QString str2 = C("仟佰拾万仟佰拾亿仟佰拾万仟佰拾元");
  4.     static const QString str2 = QString::fromUtf8("\344\273\237\344\275\260\346\213\276\344\270\207\344\273\237\344\275\260\346\213\276\344\272\277\344\273\237\344\275\260\346\213\276\344\270\207\344\273\237\344\275\260\346\213\276\345\205\203");
  5.     //static QString str3 = C("零壹贰叁肆伍陆柒捌玖");
  6.     static const QString str3 = QString::fromUtf8("\351\233\266\345\243\271\350\264\260\345\217\201\350\202\206\344\274\215\351\231\206\346\237\222\346\215\214\347\216\226");
  7.     static const QString strYiWan = QString::fromUtf8("\344\272\277\344\270\207");//= C("亿万");
  8.     static const QString strZero = QString::fromUtf8("\351\233\266");// = C("零");
  9.     static const QString strZero2 = QString::fromUtf8("\351\233\266\351\233\266");// = C("零零");
  10.     static const QString strYuan = QString::fromUtf8("\345\205\203");// = C("元");
  11.     static const QString strZheng = QString::fromUtf8("\346\225\264");// = C("整");
  12.     static const QString strJiao = QString::fromUtf8("\350\247\222");// = C("角");
  13.     static const QString strFen = QString::fromUtf8("\345\210\206");// = C("分");
  14.     static const QString strJiaoZheng = QString::fromUtf8("\350\247\222\346\225\264");// = C("角整");
  15.     QString str = bcadd(number, QString("0.00"), 2);
  16.     str.replace("-", "");
  17.     int cnt = str3.size();
  18.     QStringList str3arr;
  19.     for (int i = 0; i < cnt; i++)
  20.     {
  21.         str3arr << str3.mid(i, 1);
  22.     }
  23.     int len2 = str.size() - 3;
  24.     QString str22 = str2.mid(str2.size() - len2);
  25.     QString str4 = "";
  26.     for (int i = 0; i < len2; i++)
  27.     {
  28.         int j = str.mid(i, 1).toInt();
  29.         QString word_unit = str22.mid(i, 1);
  30.         if (strYiWan.indexOf(word_unit) > -1)
  31.         {
  32.             if (j == 0)
  33.             {
  34.                 str4 += word_unit;
  35.             }
  36.             else
  37.             {
  38.                 str4 += str3arr[j] + word_unit;
  39.             }
  40.         }
  41.         else
  42.         {
  43.             if (j == 0)
  44.             {
  45.                 str4 += strZero;//C("零");
  46.             }
  47.             else
  48.             {
  49.                 str4 += str3arr[j] + word_unit;
  50.             }
  51.         }
  52.     }
  53.     int pos;
  54.     while ((pos = str4.indexOf(strZero2)) > -1)
  55.     {
  56.         str4 = str4.mid(0, pos) + str4.mid(pos + 1);
  57.     }
  58.     if (str4.right(1) == strZero)
  59.     {
  60.         if (str4.size() > 1)
  61.         {
  62.             str4 = str4.mid(0, str4.size() - 1) + strYuan;//C("元");
  63.         }
  64.         else
  65.         {
  66.             str4 += strYuan;//C("元");
  67.         }
  68.     }
  69.     if (str.right(2) == "00")
  70.     {
  71.         str4 += strZheng;//C("整");
  72.     }
  73.     else if (str.right(1) == "0")
  74.     {
  75.         // 零分
  76.         int j = str.right(2).left(1).toInt();
  77.         str4 += str3arr[j] + strJiaoZheng;
  78.     }
  79.     else if (str.right(2).left(1) == "0")
  80.     {
  81.         // 零角
  82.         int j = str.right(1).toInt();
  83.         str4 += strZero + str3arr[j] + strFen;
  84.     }
  85.     else
  86.     {
  87.         int j = str.right(2).left(1).toInt();
  88.         str4 += str3arr[j] + strJiao;
  89.         j = str.right(1).toInt();
  90.         str4 += str3arr[j] + strFen;
  91.     }
  92.     return str4;
  93. }

二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
快速回复
限100 字节
 
上一个 下一个