zoukankan      html  css  js  c++  java
  • 数据转换


    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;

    namespace ERN.Tools.Common
    {
    public static class DecimalUtility
    {
    public static Decimal? ParseToDecimalValue(object decimalObj)
    {
    if (decimalObj == null) return null;
    Decimal decValue;
    if (!Decimal.TryParse(decimalObj.ToString(), out decValue)) return null;
    return decValue;
    }

    /// <summary>
    /// 转中文大写数字
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string ConvertNumToZHUpperCase(decimal value)
    {
    string[] numList = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
    string[] unitList = { "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟" };

    decimal money = value;
    if (money == 0)
    {
    return "零元整";
    }

    StringBuilder strMoney = new StringBuilder();
    //只取小数后2位

    string strNum = decimal.Truncate(money * 100).ToString();

    int len = strNum.Length;
    int zero = 0;
    for (int i = 0; i < len; i++)
    {
    int num = int.Parse(strNum.Substring(i, 1));
    int unitNum = len - i - 1;

    if (num == 0)
    {
    zero++;
    if (unitNum == 2 || unitNum == 6 || unitNum == 10)
    {
    if (unitNum == 2 || zero < 4)
    strMoney.Append(unitList[unitNum]);
    zero = 0;
    }
    }
    else
    {

    if (zero > 0)
    {
    strMoney.Append(numList[0]);
    zero = 0;
    }
    strMoney.Append(numList[num]);
    strMoney.Append(unitList[unitNum]);
    }

    }
    if (zero > 0)
    strMoney.Append("整");

    return strMoney.ToString();
    }

    /// <summary>
    /// 截取指定位数
    /// </summary>
    /// <param name="d"></param>
    /// <param name="s"></param>
    /// <returns></returns>
    public static decimal ToFixed(decimal d, int s)
    {
    decimal sp = Convert.ToDecimal(Math.Pow(10, s));
    return Math.Truncate(d) + Math.Floor((d - Math.Truncate(d)) * sp) / sp;
    }

    /// <summary>
    /// 截取指定位数
    /// </summary>
    /// <param name="d"></param>
    /// <param name="s"></param>
    /// <returns></returns>
    public static double ToFixed(double d, int s)
    {
    double sp = Math.Pow(10, s);
    return Math.Truncate(d) + Math.Floor((d - Math.Truncate(d)) * sp) / sp;
    }
    }
    }

  • 相关阅读:
    zabbix监控
    ipv4固定ip地址
    CentOS7 内核优化 修改参数
    流媒体服务器 red5
    linux安装git
    zabbix-钉钉报警媒介
    Windows下利用IIS建立网站并实现局域网共享
    docker的简单操作和端口映射
    docker概述和安装及基本操作
    VMware Workstation创建Windows2012server虚拟机
  • 原文地址:https://www.cnblogs.com/lockzy/p/11759017.html
Copyright © 2011-2022 走看看