// For example: (new Money(200)).ToString() == "200 Yuan"
namespace {
using ;
class Test {
static void Main() {
for (;;) {
("Amount: ");
string s = ();
decimal m;
try {
m = (s);
} catch {
break;
}
("Uppercase: " + new Money(m));
}
}
}
// The ToString() method overloaded by this class returns a capital amount string
class Money {
public string Yuan = "yuan"; // "yuan", can be changed to "circle" or "rouble" or so
public string Jiao = "corner"; // "corner", can be changed to "select"
public string Fen = "Fin"; // "Fin", can be changed to "Cent" or something like
static string Digit = "Zero One, Two Three, Four, Five, and Four, and Four, and eight nine"; // Capitalized numbers
bool isAllZero = true; // Is it all zero in the fragment
bool isPreZero = true; // Is the low digit zero?
bool Overflow = false; // Overflow flag
long money100; // Amount *100, that is, the amount in units of "minimum"
long value; // Absolute value of money100
StringBuilder sb = new StringBuilder(); // uppercase amount string, reverse order
// Read-only attribute: "zero element"
public string ZeroString {
get {
return Digit[0] + Yuan;
}
}
// Constructor
public Money(decimal money) {
try {
money100 = (long)(money * 100m);
} catch {
Overflow = true;
}
if (money100 == ) Overflow = true;
}
// Overload the ToString() method to return the uppercase amount string
public override string ToString() {
if (Overflow) return "Amount out of range";
if (money100 == 0) return ZeroString;
string[] Unit = {
Yuan,
"Ten thousand",
"100 million",
"Ten thousand",
"Billion Billion"
};
value = (money100);
ParseSection(true);
for (int i = 0; i < && value > 0; i++) {
if (isPreZero && !isAllZero) (Digit[0]);
if (i == 4 && ().EndsWith(Unit[2])) ( - Unit[2].Length, Unit[2].Length);
(Unit[i]);
ParseSection(false);
if ((i % 2) == 1 && isAllZero) ( - Unit[i].Length, Unit[i].Length);
}
if (money100 < 0) ("Negative");
return Reverse();
}
// Analyze "fragment": "angle fraction (2 digits)" or "a segment within ten thousand (4 digits)"
void ParseSection(bool isJiaoFen) {
string[] Unit = isJiaoFen ? new string[] {
Fen,
Jiao
}: new string[] {
"",
"Select",
"Bai",
"Qian"
};
isAllZero = true;
for (int i = 0; i < && value > 0; i++) {
int d = (int)(value % 10);
if (d != 0) {
if (isPreZero && !isAllZero) (Digit[0]);
("{0}{1}", Unit[i], Digit[d]);
isAllZero = false;
}
isPreZero = (d == 0);
value /= 10;
}
}
// Invert the string
string Reverse() {
StringBuilder sbReversed = new StringBuilder();
for (int i = - 1; i >= 0; i--) (sb[i]);
return ();
}
}
}