思路:
  中文对金额的描述以四位为一组,
  只考虑一万亿以内的数字则每组内以千、百、十和[亿\万\元]区分各位
  连续的零按一个处理,组内最低位的零可略去
  无角无分说整,有角无分只说角,无角有分说零X分,有角有分...

代码:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace MorrisSpace
{
/// <summary>
/// 中文金额字符串辅助类。Helper for Amount string in Chinese
/// </summary>
public class AmountStringHelper
{
static private readonly char[] units = { '分', '角', '拾', '佰', '仟', '圆', '万', '亿', '整' };
// 0 1 2 3 4 5 6 7 8
static private readonly char[] numbers = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' }; /// <summary>
/// 数字金额转大写金额
/// </summary>
/// <param name="num">金额数字</param>
/// <returns>大写金额字符串</returns>
public static string GetAmountInWords(double num)
{
double amount = Math.Round(num, );
long integ = (int)amount;
double fract = Math.Round(amount - integ, );
if (integ.ToString().Length > )
{
return null;
}
string result = "";
if (fract - 0.0 != )
{
string tempstr = fract.ToString();
if (tempstr.Length == )
{
result += numbers[(int)(fract * )];
result += units[];
}
else
{
int frist = (int)(fract * );
int second = (int)(fract * - frist * );
if (frist != )
{
result += numbers[frist];
result += units[];
result += numbers[second];
result += units[];
}
else
{
result += numbers[];
result += numbers[second];
result += units[];
}
}
}
else
{
result += units[];
} for (int temp = (int)(integ % ), secnum = ; temp != ; temp = (int)(integ % ), secnum++)
{
result = FourBitTrans(temp) + units[secnum + ] + result;
integ /= ;
if (integ != && temp < )
{
result = numbers[] + result;
}
}
return result;
} /// <summary>
/// 进行四位数字转换的辅助函数
/// </summary>
/// <param name="num">四位以下数字</param>
/// <returns>大写金额四位节</returns>
public static string FourBitTrans(int num)
{
string tempstr = num.ToString();
if (tempstr.Length > )
{
return null;
}
string result = string.Empty;
int i = tempstr.Length;
int j = ;
bool zeromark = true;
while (--i >= )
{
j++; if (tempstr[i] == '')
{
if (zeromark == true)
{
continue;
}
zeromark = true;
result = numbers[] + result;
continue;
}
zeromark = false;
if (j > )
{
result = units[j] + result;
}
int temp = tempstr[i] - '';
result = numbers[temp] + result;
}
return result;
} }
}

--------------------------------------

这个代码只适合一亿以内的金额,但相信以满足绝大多数情况

大写金额字符串生成 C#实现的更多相关文章

  1. Excel 数值转换为人民币大写金额字符串

    把$B$27单元格中的数值转换为人民币大写金额字符串: 目标单元格填入以下公式: =IF($B$27=0,CONCATENATE(IF($B$27<=0,,TEXT(INT($B$27),&qu ...

  2. php 依据字符串生成相应数组方法

    php 依据字符串生成相应数组方法 比如: <?php $config = array( 'project|page|index' => 'content', 'project|page| ...

  3. 使用wcf编写坐标字符串生成shapefile文件,在iis发布供前端调用

    项目有一需求,需要由坐标字符串(格式:x,y,点名)生成shapefile,由于在前台开发类似功能比较麻烦而且也不适用,最终决定使用WCF来实现,不借助现有GIS软件,基于GDAL实现. 实现过程如下 ...

  4. QT json字符串生成和解析

    1         QT json字符串生成和解析 1.1  QT Json解析流程 (1)  字符串转化为QJsonDocument QJsonParseError json_error; QJso ...

  5. Python之将Python字符串生成PDF

      笔者在今天的工作中,遇到了一个需求,那就是如何将Python字符串生成PDF.比如,需要把Python字符串'这是测试文件'生成为PDF, 该PDF中含有文字'这是测试文件'.   经过一番检索, ...

  6. Javascript 解析字符串生成 XML DOM 对象。

    Javascript 接收字符串生成 XML DOM 对象.实测对 Firefox .IE6 有效.可用于解析 ajax 的服务器响应结果,也可用于解析自定义字符串.​1. [代码]函数   ppt模 ...

  7. c#实例化继承类,必须对被继承类的程序集做引用 .net core Redis分布式缓存客户端实现逻辑分析及示例demo 数据库笔记之索引和事务 centos 7下安装python 3.6笔记 你大波哥~ C#开源框架(转载) JSON C# Class Generator ---由json字符串生成C#实体类的工具

    c#实例化继承类,必须对被继承类的程序集做引用   0x00 问题 类型“Model.NewModel”在未被引用的程序集中定义.必须添加对程序集“Model, Version=1.0.0.0, Cu ...

  8. java 与日期转换相关的方法(java.util.date类型和java.sql.date类型互相转换)、随机字符串生成方法、UUID生产随机字符串

    package com.oop.util; import java.text.*; import java.util.UUID; import org.junit.Test; /* * 与日期相关的工 ...

  9. java读取字符串,生成txt文件

    /** * 读取字符串,生成txt 文件 已解决未设置编码时,在项目中直接打开文件,中文乱码问题 * WriteText.writeToText(musicInfo,fileName)直接调用 * * ...

随机推荐

  1. ASP.NET 让无码编程不在是梦 -.NET通用平台、通用权限、易扩展、多语言、多平台架构框架

    先拿出我半前年前平台的设计初稿,经过半年的努力我已经完成了该设计稿的所有功能.并且理念已经远远超出该设计稿. 下面是一些博友对我贴子的评价: 1.楼主,想法很美好,现实很骨感,我们公司就有一套你说的这 ...

  2. 警惕SQL语句陷井

    以下SQL段,大家认为结果是什么呢? DECLARE @A VARCHAR(50) SET @A='Zuowenjun.cn' SELECT TOP 1 @A=ISNULL(FIELDNAME,'DE ...

  3. MyBatis魔法堂:ResultMap详解

    一.前言   MyBatis是基于“数据库结构不可控”的思想建立的,也就是我们希望数据库遵循第三范式或BCNF,但实际事与愿违,那么结果集映射就是MyBatis为我们提供这种理想与现实间转换的手段了, ...

  4. sprint3(第二天)

    今天完成的任务有统计用户,全局管理员可以对员工或者用户设置权限. 燃尽图

  5. Mybatis 示例之 foreach

    foreach一共有三种类型,分别为List,[](array),Map三种. foreach的第一篇用来将List和数组(array). 下面表格是我总结的各个属性的用途和注意点. foreach属 ...

  6. 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774

    Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...

  7. [CLR via C#]14. 字符、字符串和文本处理

    一.字符 在.NET Framewole中,字符总是表示成16位Unicode代码值,这简化了国际化应用程序的开发. 每个字符都表示成System.Char结构(一个值类型) 的一个实例.System ...

  8. [CLR via C#]9. 参数

    一.可选参数和命名参数 在设计一个方法的参数时,可为部分或全部参数分配默认值.然后,调用这些方法的代码时可以选择不指定部分实参,接受默认值.此外,调用方法时,还可以通过指定参数名称的方式为其传递实参. ...

  9. 重新想象 Windows 8 Store Apps (53) - 绑定: 与 ObservableCollection CollectionViewSource VirtualizedFilesVector VirtualizedItemsVector 绑定

    [源码下载] 重新想象 Windows 8 Store Apps (53) - 绑定: 与 ObservableCollection CollectionViewSource VirtualizedF ...

  10. MyEclipse+Mysql (一)

     MyEclipse连接Mysql数据库   准备工作:MyEclipse使用的是2013版,mysql  Ver 14.14 Distrib 5.6.28   1.jar包的下载(jdbc驱动) 我 ...