You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace POSV.Utils
{
public class DecimalUtils
{
/// <summary>
/// decimal保留指定位数小数
/// </summary>
/// <param name="num">原始数量</param>
/// <param name="scale">保留小数位数</param>
/// <returns>截取指定小数位数后的数量字符串</returns>
public static decimal ToRound(decimal num , int scale)
{
string strDecimal = Math.Round(num , scale , MidpointRounding.AwayFromZero).ToString();
int index = strDecimal.IndexOf(".");
if (index == -1 || strDecimal.Length < index + scale + 1)
{
strDecimal = string.Format("{0:F" + scale + "}" , num);
}
else
{
int length = index;
if (scale != 0)
{
length = index + scale + 1;
}
strDecimal = strDecimal.Substring(0 , length);
}
decimal result;
decimal.TryParse(strDecimal , out result);
return result;
}
public static decimal Fen2Yuan(object amount)
{
return Fen2Yuan(StringUtils.GetDecimal(amount));
}
public static decimal Fen2Yuan(decimal amount)
{
return amount / 100;
}
/// <summary>
/// 元转分
/// </summary>
/// <param name="amount"></param>
/// <returns></returns>
public static string Yuan2Fen(decimal amount)
{
return Convert.ToInt64(amount * 100).ToString();
}
}
}