using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace POSV.Utils { public class DecimalUtils { /// /// decimal保留指定位数小数 /// /// 原始数量 /// 保留小数位数 /// 截取指定小数位数后的数量字符串 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; } /// /// 元转分 /// /// /// public static string Yuan2Fen(decimal amount) { return Convert.ToInt64(amount * 100).ToString(); } } }