程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> 大寫金額轉換成小寫

大寫金額轉換成小寫

編輯:關於.NET

Q:如何將大寫金額轉換成小寫?

A:簡單點,格式比較嚴格

private float getMoney(string strBig)
    ...{
      string strExp="";
      string[] arrBig = new string[] ...{ "整","","零","+0","壹","+1","貳","+2","三","+3","肆","+4","伍","+5","陸","+6","柒","+7","捌","+8","玖","+9", "分","*0.01","角","*0.1","元","*1","拾","*10.00","佰","*100.00","仟","*1000.00","萬","*10000.00","億","*100000000.00"};
      for (int i = 0; i < strBig.Length; i++)
      ...{
        for (int j = 0; j < arrBig.Length-1;j+=2 )
        ...{
          if (arrBig[j] == strBig[i].ToString())
          ...{
            strExp += arrBig[j + 1].ToString();
            continue;
          }
        }
      }
  
      object ret=new DataTable().Compute(strExp, "");
      return float.Parse(ret.ToString());
      //Console.WriteLine(getMoney("玖仟零貳拾壹元陸角肆分"));
      //Console.WriteLine(getMoney("玖仟萬億零貳拾壹元陸角肆分"));
    }

伴水兄的這個較狠

 

 

public double DecodeMoneyCn(string AText)
...{
  AText = AText.Replace("萬萬", "億");
  AText = AText.Replace("點", "元");
  AText = AText.Replace("塊", "元");
  AText = AText.Replace("毛", "角");
  double vResult = 0;
  double vNumber = 0; // 當前數字
  double vTemp = 0;
  int vDecimal = 0; // 是否出現小數點
  foreach (char vChar in AText)
  ...{
    int i = "零一二三四五六七八九".IndexOf(vChar);
    if (i < 0) i = "零幺兩三四五六七八九".IndexOf(vChar);
    if (i < 0) i = "零壹貳三肆伍陸柒捌玖".IndexOf(vChar);
    if (i > 0)
    ...{
      vNumber = i;
      if (vDecimal > 0)
      ...{
        vResult += vNumber * Math.Pow(10, -vDecimal);
        vDecimal++;
        vNumber = 0;
      }
    }
    else
    ...{
      i = "元十百千萬億".IndexOf(vChar);
      if (i < 0) i = "整拾佰仟萬億".IndexOf(vChar);
      if (i == 5) i = 8;
      if (i > 0)
      ...{
        if (i >= 4)
        ...{
          vTemp += vNumber;
          if (vTemp == 0) vTemp = 1;
          vResult += vTemp * Math.Pow(10, i);
          vTemp = 0;
        }
        else vTemp += vNumber * Math.Pow(10, i);
      }
      else
      ...{
        i = "元角分".IndexOf(vChar);
        if (i > 0)
        ...{
          vTemp += vNumber;
          vResult += vTemp * Math.Pow(10, -i);
          vTemp = 0;
        }
        else if (i == 0)
        ...{
          vTemp += vNumber;
          vResult += vTemp;
          vDecimal = 1;
          vTemp = 0;
        }
      }
      vNumber = 0;
    }
  }
  return vResult + vTemp + vNumber;
}
  
private void button2_Click(object sender, EventArgs e)
...{
  string[] vTestText = ...{
    "十二點五六",
    "一億零一萬零五",
    "四萬萬",
    "九十八億七千六百五十四萬三千二百一十",
    "五元一角四分", "壹佰元整",
    "三千五百萬",
    "九塊二毛"};
  foreach (string vText in vTestText)
  ...{
    Console.WriteLine("DecodeMoneyCn("{0}")={1}", vText,
      DecodeMoneyCn(vText));
  }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved