C# double和decimal數(shù)據(jù)類型以截?cái)嗟姆绞奖A糁付ǖ男?shù)位數(shù)
更新時(shí)間:2012年05月23日 15:58:47 作者:
從事ASP.NET in C#開發(fā)快一年了,今天才知道,C#中保留小數(shù)位數(shù)時(shí)沒(méi)有使用截?cái)嗟姆绞?/div>
項(xiàng)目中要用到以截?cái)嗟姆绞饺⌒?shù)點(diǎn)后兩位,故寫了以下方法:
/// <summary>
/// 將小數(shù)值按指定的小數(shù)位數(shù)截?cái)?
/// </summary>
/// <param name="d">要截?cái)嗟男?shù)</param>
/// <param name="s">小數(shù)位數(shù),s大于等于0,小于等于28</param>
/// <returns></returns>
public static decimal ToFixed(decimal d, int s)
{
decimal sp = Convert.ToDecimal(Math.Pow(10, s));
if (d < 0)
return Math.Truncate(d) + Math.Ceiling((d - Math.Truncate(d)) * sp) / sp;
else
return Math.Truncate(d) + Math.Floor((d - Math.Truncate(d)) * sp) / sp;
}
/// <summary>
/// 將雙精度浮點(diǎn)值按指定的小數(shù)位數(shù)截?cái)?
/// </summary>
/// <param name="d">要截?cái)嗟碾p精度浮點(diǎn)數(shù)</param>
/// <param name="s">小數(shù)位數(shù),s大于等于0,小于等于15</param>
/// <returns></returns>
public static double ToFixed(double d, int s)
{
double sp = Math.Pow(10, s);
if (d < 0)
return Math.Truncate(d) + Math.Ceiling((d - Math.Truncate(d)) * sp) / sp;
else
return Math.Truncate(d) + Math.Floor((d - Math.Truncate(d)) * sp) / sp;
}
順帶提一下:
double和decimal的ToString("#.##")方法使用的是四舍五入;
靜態(tài)類System.Math下的Round(decimal d, int decimals)方法,舍入的方式使用的是“四舍六入五成雙”;
靜態(tài)類System.Math下的Round(decimal d, int decimals, MidpointRounding mode)的第三個(gè)參數(shù)是枚舉參數(shù),指示如何處理中間值(5);
靜態(tài)類System.Math的方法:http://msdn.microsoft.com/zh-cn/library/system.math_methods(v=vs.80)
復(fù)制代碼 代碼如下:
/// <summary>
/// 將小數(shù)值按指定的小數(shù)位數(shù)截?cái)?
/// </summary>
/// <param name="d">要截?cái)嗟男?shù)</param>
/// <param name="s">小數(shù)位數(shù),s大于等于0,小于等于28</param>
/// <returns></returns>
public static decimal ToFixed(decimal d, int s)
{
decimal sp = Convert.ToDecimal(Math.Pow(10, s));
if (d < 0)
return Math.Truncate(d) + Math.Ceiling((d - Math.Truncate(d)) * sp) / sp;
else
return Math.Truncate(d) + Math.Floor((d - Math.Truncate(d)) * sp) / sp;
}
/// <summary>
/// 將雙精度浮點(diǎn)值按指定的小數(shù)位數(shù)截?cái)?
/// </summary>
/// <param name="d">要截?cái)嗟碾p精度浮點(diǎn)數(shù)</param>
/// <param name="s">小數(shù)位數(shù),s大于等于0,小于等于15</param>
/// <returns></returns>
public static double ToFixed(double d, int s)
{
double sp = Math.Pow(10, s);
if (d < 0)
return Math.Truncate(d) + Math.Ceiling((d - Math.Truncate(d)) * sp) / sp;
else
return Math.Truncate(d) + Math.Floor((d - Math.Truncate(d)) * sp) / sp;
}
順帶提一下:
double和decimal的ToString("#.##")方法使用的是四舍五入;
靜態(tài)類System.Math下的Round(decimal d, int decimals)方法,舍入的方式使用的是“四舍六入五成雙”;
靜態(tài)類System.Math下的Round(decimal d, int decimals, MidpointRounding mode)的第三個(gè)參數(shù)是枚舉參數(shù),指示如何處理中間值(5);
靜態(tài)類System.Math的方法:http://msdn.microsoft.com/zh-cn/library/system.math_methods(v=vs.80)
相關(guān)文章
關(guān)于Unity C# Mathf.Abs()取絕對(duì)值性能測(cè)試詳解
這篇文章主要給大家介紹了關(guān)于Unity C# Mathf.Abs()取絕對(duì)值性能測(cè)試的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Unity C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04實(shí)例詳解C#實(shí)現(xiàn)http不同方法的請(qǐng)求
本篇文章給大家分享了C#實(shí)現(xiàn)http不同方法的請(qǐng)求的相關(guān)知識(shí)點(diǎn)以及實(shí)例代碼,有需要的朋友參考下。2018-07-07