思路:
  中文对金额的描述以四位为一组,
  只考虑一万亿以内的数字则每组内以千、百、十和[亿\万\元]区分各位
  连续的零按一个处理,组内最低位的零可略去
  无角无分说整,有角无分只说角,无角有分说零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. SQL Server中的事务日志管理(5/9):完整恢复模式里的日志管理

    当一切正常时,没有必要特别留意什么是事务日志,它是如何工作的.你只要确保每个数据库都有正确的备份.当出现问题时,事务日志的理解对于采取修正操作是重要的,尤其在需要紧急恢复数据库到指定点时.这系列文章会 ...

  2. 表上的DELETE操作

    在今天的文章里,我想给你快速展示下当我们从表里删除记录时,在SQL Server里发生了什么.首先我们来创建一个简单的表,在8KB的页上刚好能插入4条记录. -- Create a simple ta ...

  3. sitemesh学习笔记(1)

    最近在学习web开发的时候,发现很多的页面都存在同样的导航栏,登陆栏,js,jQuery等等相同的元素.这样就感觉开发变得好臃肿啊,并且,有时候改一个元素,就要把所有包含这个元素的页面全部重新码一遍, ...

  4. String-------RegularHelper

    /// <summary> /// 正则表达式相关方法集合 /// </summary> public static class RegularHelper { private ...

  5. SQL Server获取下一个编码字符实现

    周末看到SQL Server 大V潇湘隐者的获取下一个编码字符串问题,本来作为以上博文的回复,也许回复内容长度超过其允许限制,无法提交.鉴于此,特记录SQL Server实现过程,方便自己回顾和查阅. ...

  6. 译:什么是ViewData的, ViewBag和TempData? - MVC为当前和后续请求之间传递数据的三种方法

    译文出处:http://www.codeproject.com/Articles/476967/WhatplusisplusViewData-cplusViewBagplusandplusTem AS ...

  7. 剑指offer面试题30:最小的k个数

    一.题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 二.解题思路 1.思路1 首先对数组进行排序,然后取出前k个数 ...

  8. js不间断滚动

    CSS ul, li { margin: 0; padding: 0; } #scrollDiv { width: 300px; height: 25px; line-height: 25px; bo ...

  9. csharp:引入app.manifest,程序在win7下以管理员权限运行配置方法

    https://msdn.microsoft.com/en-us/library/windows/desktop/hh848036(v=vs.85).aspx https://msdn.microso ...

  10. Bootstrap 框架 栅格布局系统设计原理

    如果你是初次接触Bootstrap,你一定会为它的栅格布局感到敬佩.事实上,这个布局系统提供了一套响应式的布局解决方案. 既然这么好用,那他是如何用CSS来实现的呢? 我特意去Bootstrap官方下 ...