zoukankan
html css js c++ java
格式化为中文数字
namespace
Microshaoft
{
using
System;
public
class
ChineseFormat : System.ICustomFormatter, System.IFormatProvider
{
//
如果format Type与当前实例类型相同,则为当前实例,否则为空引用
public
object
GetFormat(Type format)
{
if
(format
==
typeof
(ICustomFormatter))
{
return
this
;
}
return
null
;
}
//
实现Format方法说明:
//
如果您的格式方法不支持格式,则确定正在设置格式的对象是否实现 IFormattable 接口。
//
如果实现,请调用该接口的IFormattable.ToString 方法。
//
否则,调用基础对象的默认 Object.ToString 方法。
public
string
Format(
string
format,
object
arg, IFormatProvider provider)
{
if
(format
==
null
)
{
if
(arg
is
IFormattable)
{
return
((IFormattable) arg).ToString(format, provider);
}
return
arg.ToString();
}
else
{
if
(format
==
"
ChineseFormat
"
)
{
string
[] Nums
=
new
string
[]
{
"
零
"
,
"
壹
"
,
"
贰
"
,
"
叁
"
,
"
肆
"
,
"
伍
"
,
"
陆
"
,
"
柒
"
,
"
捌
"
,
"
玖
"
}
;
//
位 数组
string
[] Digits
=
new
string
[]
{
""
,
"
拾
"
,
"
佰
"
,
"
仟
"
}
;
//
单位 数组
string
[] Units
=
new
string
[]
{
""
,
"
[万]
"
,
"
[亿]
"
,
"
[万亿]
"
}
;
return
ConvertNumberToChinese(arg.ToString(), Nums, Digits, Units);
//
return "***"+arg.ToString();
}
else
{
if
(arg
is
IFormattable)
{
return
((IFormattable) arg).ToString(format, provider);
}
return
arg.ToString();
}
}
}
public
static
string
ConvertNumberToChinese(
string
x,
string
[] Nums,
string
[] Digits,
string
[] Units)
{
string
S
=
""
;
//
返回值
int
p
=
0
;
//
字符位置指针
int
m
=
x.Length
%
4
;
//
取模
//
四位一组得到组数
int
k
=
(m
>
0
?
x.Length
/
4
+
1
: x.Length
/
4
);
//
外层循环在所有组中循环
//
从左到右 高位到低位 四位一组 逐组处理
//
每组最后加上一个单位: "[万亿]","[亿]","[万]"
for
(
int
i
=
k; i
>
0
; i
--
)
{
int
L
=
4
;
if
(i
==
k
&&
m
!=
0
)
{
L
=
m;
}
//
得到一组四位数 最高位组有可能不足四位
string
s
=
x.Substring(p, L);
int
l
=
s.Length;
//
内层循环在该组中的每一位数上循环 从左到右 高位到低位
for
(
int
j
=
0
; j
<
l; j
++
)
{
//
处理改组中的每一位数加上所在位: "仟","佰","拾",""(个)
int
n
=
Convert.ToInt32(s.Substring(j,
1
));
if
(n
==
0
)
{
if
(j
<
l
-
1
&&
Convert.ToInt32(s.Substring(j
+
1
,
1
))
>
0
//
后一位(右低)
&&
!
S.EndsWith(Nums[n]))
{
S
+=
Nums[n];
}
}
else
{
//
处理 1013 一千零"十三", 1113 一千一百"一十三"
if
(
!
(n
==
1
&&
(S.EndsWith(Nums[
0
])
|
S.Length
==
0
)
&&
j
==
l
-
2
))
{
S
+=
Nums[n];
}
S
+=
Digits[l
-
j
-
1
];
}
}
p
+=
L;
//
每组最后加上一个单位: [万],[亿] 等
if
(i
<
k)
//
不是最高位的一组
{
if
(Convert.ToInt32(s)
!=
0
)
{
//
如果所有 4 位不全是 0 则加上单位 [万],[亿] 等
S
+=
Units[i
-
1
];
}
}
else
{
//
处理最高位的一组,最后必须加上单位
S
+=
Units[i
-
1
];
}
}
return
S;
}
}
}
namespace
Test
{
using
System;
using
Microshaoft;
class
AppTest
{
static
void
Main()
{
string
printString
=
String.Empty;
long
i
=
1100000013
;
ChineseFormat fmt
=
new
ChineseFormat();
printString
=
string
.Format(fmt,
"
显示正常格式: {0}
"
, i);
Console.WriteLine(printString);
printString
=
string
.Format(fmt,
"
显示正常格式: {0:C}
"
, i);
Console.WriteLine(printString);
printString
=
string
.Format(fmt,
"
显示自定义格式: {0:ChineseFormat}
"
, i);
Console.WriteLine(printString);
Console.ReadLine();
}
}
}
/************************************************/
本博客内容如果是原著都会在标题后加上(原著)字样,未加者多数为转载.
/************************************************/
查看全文
相关阅读:
<a href=”#”>与 <a href=”javascript:void(0)” 的区别
win7与ubuntu11.04双系统
winform中为控件设置鼠标样式
如何得到格林威治标准时间(C#,SQL SERVER)
winform中将光标定位到多行文本框的最下处
VS2010 MV3开发时让jQuery支持智能感知
转载:SQL Server 2005无日志文件附加数据库
PowerPoint无法显示文件中某些幻灯片中的文字、图像或对象
转载:版本简介
使用 ADO.NET连接SQL Azure
原文地址:https://www.cnblogs.com/ghfsusan/p/1407017.html
最新文章
Ubuntu 11.04安装NVIDIA官方驱动的方法
vc 获取屏幕大小
Ubuntu 11.04 下安装配置 JDK 7
在ubuntu在已经可以识别手机,但eclipse不能识别
立体视觉:基于OpenGL的立体图像对的软件实现方法
Ubuntu Android NDK环境的配置
如何在Android中使用OpenCV
找不到MSVCR100.DLL解决办法
HO引擎近况20211129
rspec时出现 undefined method
热门文章
rails程序运行顺序小探索
attr_accessible
rspec Could not find table
rake db:seed实例
ASP 数据库连接出错ACCESS
ruby中双引号与单引号的区别
jQuery选择器实例手册
url与path的区别
强制安装chrome扩展程序
使用DataAnnotations实现数据验证
Copyright © 2011-2022 走看看