用C#语言对于用户从键盘输入的一个数字,编程输出这一数字的人民币金

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用C#语言对于用户从键盘输入的一个数字,编程输出这一数字的人民币金相关的知识,希望对你有一定的参考价值。

用C#语言对于用户从键盘输入的一个数字,编程输出这一数字的人民币金额大写
例如1234567.89输出 壹佰贰拾叁万肆仟伍佰陆拾柒圆捌角玖分整

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace ConsoleApplication1

class Print

const int ZS = 0;

const int XS = 1;


public static void Main(string[] args)

 

Print print = new Print();

string strWord;

while (true )

string strNum = Console.ReadLine();

strWord = print.PrintWord(strNum);

Console.WriteLine(strWord);

/// <summary>

/// 打印

/// </summary>

/// <param name="num"></param>

/// <returns></returns>

private string PrintWord(string num)

string zhengShu;

string xiaoShu;

string zhengWord;

string xiaoWord;

switch (num.Split('.').Length)

case 0:

return "未知错误";

case 1:

zhengShu = num.Split('.')[0];

xiaoShu = "";

break;

case 2:

zhengShu = num.Split('.')[0];

xiaoShu = num.Split('.')[1];

break;

default:

return "输入的数字格式不正确";

zhengWord = PrintZS(zhengShu);

xiaoWord = PrintXS(xiaoShu);

return (zhengWord==""?"零": zhengWord) + "圆"+ xiaoWord + "整";

/// <summary>

/// 打印整数部分

/// </summary>

/// <param name="zs"></param>

/// <returns></returns>

private string PrintZS(string zs)

//最大表示到千亿

if (zs.Length > 12)

return "整数部分过长";

List<string> str = new List<string>();

StringBuilder strbZS = new StringBuilder();

if (zs.Length >= 4)

do

str.Add(zs.Substring(zs.Length - 4, 4));

zs = zs.Substring(0, zs.Length - 4);


while (zs.Length > 4);

if(zs.Length !=0)

str.Add(zs);


int countAll = 0;

for(int i= str.Count-1; i>=0;i--)

int printCount = 0;

for (int j = 0; j < str[i].Length; j++)

 

if(str[i][j]!='0')

strbZS.Append(PrintNum(str[i][j]));

strbZS.Append(PrintChildUnit(str[i].Length - j, ZS));

printCount++;

else

if (i== str.Count - 1)

if (j != 0)

int count = 0;

for (int k = 0; k < j; k++)

if (str[i][k] == '0')

count++;

if (count != j && j != str[i].Length - 1 && str[i][j + 1] != '0')

strbZS.Append(PrintNum(str[i][j]));

printCount++;

else

if (countAll !=0 && j != str[i].Length - 1 && str[i][j + 1] != '0')

strbZS.Append(PrintNum(str[i][j]));

printCount++;

     

if (printCount != 0)

strbZS.Append(PrintUnit(i + 1));

countAll++;

return strbZS.ToString();

/// <summary>

/// 打印小数部分

/// </summary>

/// <param name="xs"></param>

/// <returns></returns>

private string PrintXS(string xs)

//最小表示到毫

if (xs.Length > 4)

return "小数部分过长";

StringBuilder strbXS = new StringBuilder();

for(int i = 0; i < xs.Length; i++)

strbXS.Append(PrintNum(xs[i]));

strbXS.Append(PrintChildUnit(i+1,XS));

return strbXS.ToString();

/// <summary>

/// 打印数字

/// </summary>

/// <param name="charNum"></param>

/// <returns></returns>

private string PrintNum(char charNum)

switch (charNum)

case '0': return"零"; 

case '1': return"壹";

case '2': return"贰";

case '3': return"叁";

case '4': return"肆";

case '5': return"伍";

case '6': return"陆";

case '7': return"柒";

case '8': return"捌";

case '9': return"玖";

default:return "";

/// <summary>

/// 打印子单位

/// </summary>

/// <param name="count"></param>

/// <param name="category"></param>

/// <returns></returns>

private string PrintChildUnit(int count,int category)

//整数部分

if (category== ZS)

switch (count)

case 1: return "";

case 2: return "十";

case 3: return "百";

case 4: return "千";

default: return "";

//小数部分

else

switch (count)

case 4: return "毫";

case 3: return "厘";

case 2: return "分";

case 1: return "角";

default: return "";

/// <summary>

/// 打印单位

/// </summary>

/// <param name="count"></param>

/// <returns></returns>

private string PrintUnit(int count)

switch (count)

case 1: return "";

case 2: return "万";

case 3: return "亿";

case 4: return "兆";

default: return "";

参考技术A 控制台代码
public class GenericTest

static void Main(string[] attr)

Console.WriteLine("......");
string inputStr = Console.ReadLine() ;
string outStr = "";

double monenyInput = Convert.ToDouble(inputStr);
int money = (int)monenyInput;
int minMoney = (int)((monenyInput - money) * 100);

int money1 = money % 10000; money = money / 10000;
int money2 = money % 10000; money = money / 10000;
int money3 = money % 10000; money = money / 10000;

outStr = GetValue(money3, "亿") + GetValue(money2, "万") + GetValue(money1, "圆");

int n1 = minMoney % 10; minMoney = minMoney / 10;
int n2 = minMoney % 10; minMoney = minMoney / 10;
if (n2 > 0) outStr = outStr + GetChinese(n2, "角");
if (n1 > 0) outStr = outStr + GetChinese(n1, "分");
if (!string.IsNullOrEmpty(outStr))

outStr = outStr + "整";

Console.WriteLine(outStr);

Console.ReadLine();

static string GetValue(int num, string type)

string value = "";
if (num > 10000) return GetValue(num / 10000, type);
if (num == 0) return value;
//拆各级数字
int n1 = num % 10; num = num / 10;
int n2 = num % 10; num = num / 10;
int n3 = num % 10; num = num / 10;
int n4 = num % 10; num = num / 10;
//中文转换
value = value + GetChinese(n4, "千");
value = value + GetChinese(n3, "百");
value = value + GetChinese(n2, "十");
value = value + GetChinese(n1, "");
//去除无效的零
while (value.Contains("零零"))

value = value.Replace("零零", "零");

while (value.StartsWith("零"))

value = value.Substring(1, value.Length - 1);

while (value.EndsWith("零"))

value = value.Substring(0, value.Length - 1);

return value + type;

static string GetChinese(int num, string type)

string value = "";
switch (num)

case 0: value = "零"; break;
case 1: value = "壹" + type; break;
case 2: value = "贰" + type; break;
case 3: value = "叁" + type; break;
case 4: value = "肆" + type; break;
case 5: value = "伍" + type; break;
case 6: value = "陆" + type; break;
case 7: value = "柒" + type; break;
case 8: value = "捌" + type; break;
case 9: value = "玖" + type; break;
default: value = "零"; break;

return value;

以上是关于用C#语言对于用户从键盘输入的一个数字,编程输出这一数字的人民币金的主要内容,如果未能解决你的问题,请参考以下文章

c语言高手急救:从键盘输入长整数n,将其从个位开始,每三位数字一组用逗号间隔输出。

c语言从键盘输入一个整数,然后输出这个数字的平方和立方?

C语言编程题:从键盘输入一串字符,统计其中的数字与字母个数并输出

用c语言从键盘输入两个数字,判断哪个数更大?

想深入学习C#语言,有没有达人知道C#的好的资料呢?

C语言:编程实现从键盘输入若干学号,然后输出学号中十位数字是9的学号(输入0时结束循环)