由於在仁耀的系統中,需要將阿拉伯數字轉成國字的大寫,因此研究了一下這種功能,後來在網路上發現有高手寫好類似的程式碼 (嘿嘿~當然不客氣的拿來使用嚕,在此謝謝Q_282898034大大的開發嚕^^),以下為Q_282898034大所開發的程式:
using System;using System.Collections.Generic;static class RMB{ private static readonly string DigitText = "零壹貳參肆伍陸柒捌玖"; private static readonly string PositionText = "圓拾佰仟萬億兆京垓秭穰"; private static readonly string OtherText = "分角整負"; private static void GetFractionStack(int num, Stack
stack) { int fen, jiao = Math.DivRem(num, 10, out fen); if (fen != 0) { stack.Push(OtherText[0]); stack.Push(DigitText[fen]); } if (jiao != 0) { stack.Push(OtherText[1]); stack.Push(DigitText[jiao]); } } private static void GetIntegerStack(decimal num, int position, Stack stack) { if (num < _num =" Decimal.ToInt32(num);" i =" 0," mod_10 =" 0;" behindzero =" mod_10" _num =" Math.DivRem(_num," mod_10 ="="" _num ="=""> 0) stack.Push(PositionText[i]); stack.Push(DigitText[mod_10]); } } else { GetIntegerStack(Decimal.Remainder(num, 10000M), position, stack); int mask = -1, offset = 4; while ((position & (0x1 << ++mask)) == 0) ; mask += offset; while (stack.Peek() == PositionText[offset++]) stack.Pop(); stack.Push(PositionText[mask]); GetIntegerStack(Decimal.Divide(num, 10000M), position + 1, stack); } } public static string DecimalFormatter(Decimal input) { Stack stack = new Stack(60); bool isNegate = input < Decimal.Zero; input = Decimal.Add(isNegate ? Decimal.Negate(input) : input, 0.005M); decimal integer = Decimal.Truncate(input); int fraction = Decimal.ToInt32(Decimal.Multiply(Decimal.Subtract(input, integer), 100M)); if (fraction == 0) stack.Push(OtherText[2]); else GetFractionStack(fraction, stack); if (integer != Decimal.Zero) { stack.Push(PositionText[0]); GetIntegerStack(integer, 1, stack); if (stack.Peek() == DigitText[0]) stack.Pop(); } else if (fraction == 0) { stack.Push(PositionText[0]); stack.Push(DigitText[0]); } if (isNegate) stack.Push(OtherText[3]); return new string(stack.ToArray()); }}class Program{ static void Main() { decimal input = decimal.MaxValue; Console.WriteLine(input /= 1000M); // 国内银行通用上法转换:万万为亿,亿亿为兆 Console.WriteLine(RMB.DecimalFormatter(input)); Console.ReadKey(); }}
套到仁耀的系統上,我進行了小幅度的修改.
reference: http://topic.csdn.net/u/20080125/09/04c65875-4f50-42cd-bd63-d7b1429e42e1.html
End~
沒有留言:
張貼留言