Files
DevToolsAvalonia/常用工具集/Views/05其他/人民币转大写.axaml.cs
2025-08-26 08:37:44 +08:00

92 lines
2.6 KiB
C#

using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using System;
using System.Text.RegularExpressions;
namespace ;
public partial class : UserControl
{
public ()
{
InitializeComponent();
}
private void TextBox_KeyDown(object? sender, Avalonia.Input.KeyEventArgs e)
{
if (e.Key == Avalonia.Input.Key.Enter)
{
this.Button2_Click(sender, e);//触发button事件
}
}
private void Button_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
if (txt2.Text.Trim().Length == 0)
{
this.Button2_Click(sender, e);//触发button事件
return;
}
try
{
txt1.Text = txt1.Text.Replace(",", "");
int num = Convert.ToInt32(txt2.Text.Trim());
double rate = num * 1.0 / 100.0;
double money = Convert.ToDouble(txt1.Text);
money = money * rate;
string strMoney = Convert.ToString(money);
Show(strMoney);//触发button事件
}
catch
{
txt4.Text = "";
txt3.Text = "";
}
}
private void Button2_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
try
{
txt1.Text = txt1.Text.Replace(",", "");
decimal money = Convert.ToDecimal(txt1.Text);
string str = ConvertToChinese(money);
if (!txt1.Text.Contains("."))
str += "整";
txt4.Text = str;
txt3.Text = money.ToString("N0");
}
catch
{
txt4.Text = "";
txt3.Text = "";
}
}
public static string ConvertToChinese(decimal number)
{
var s = number.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
var d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");
var r = Regex.Replace(d, ".", m => "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟万亿兆京垓秭穰"[m.Value[0] - '-'].ToString());
return r;
}
private void Show(string money1)
{
try
{
decimal money = Convert.ToDecimal(money1);
string str = ConvertToChinese(money);
if (!txt1.Text.Contains("."))
str += "整";
txt4.Text = str;
txt3.Text = money.ToString("N0");
}
catch
{
txt4.Text = "";
txt3.Text = "";
}
}
}