using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 四舍五入小算法
{
public class MathForRoundToFourhomesfive
{
// Math.Round((decimal)526.925, 2,MidpointRounding.AwayFromZero); 原始的自带写法
/// <summary>
/// 四舍五入方法
/// </summary>
/// <auther>
/// by
/// </auther>
/// <param name="valuedata">四舍五入的原始值</param>
/// <param name="savelength">保留位数</param>
/// <returns>处理四舍五入得到的结果值</returns>
public double GetMathRoundData(double valuedata,int savelength){
bool isPoint=false;//初始化是否是小数
isPoint=valuedata.ToString().Contains(".");
bool isNegative=valuedata.ToString().Contains("-");//是否是负数
if(isNegative){
valuedata = Convert.ToDouble(valuedata.ToString().Substring(1, valuedata.ToString().Length-1));//如果是负数,则先去除“-”,之后补全
}
///1、小数
double result = GetdoubleData(valuedata, savelength,isPoint,isNegative);
///2、整数
return result;
}
/// <summary>
/// 小数类型的数据的四舍五入
/// </summary>
/// <auther>
/// by
/// </auther>
/// <param name="valuedata">四舍五入原始值</param>
/// <param name="savelength">保留位数</param>
/// <param name="isPoint">是否是小数的标识</param>
/// <param name="isNegative">是否是负数</param>
/// <returns>返回最后的结果值</returns>
public double GetdoubleData(double valuedata,int savelength,bool isPoint, bool isNegative)
{
#region 小数
if (isPoint)
{
isPoint = true;
int tempPointIndex = valuedata.ToString().IndexOf(".");//小数点的索引位
int pointLastLength = valuedata.ToString().Length - tempPointIndex - 1;//小数点之后的位数
#region 保留位为0,与非0
if (savelength == 0)
{
double tempResult = Convert.ToDouble(valuedata.ToString().Substring(0, tempPointIndex));
double tempLastValue = Convert.ToDouble(valuedata.ToString().Substring(tempPointIndex + 1, 1));
//保留位是否四舍五入
if (tempLastValue < 5)
{
tempLastValue = 0;
}
else
{
tempLastValue = 1;
}
tempResult = tempResult + tempLastValue;
if (isNegative)//是否为负数
{
return Convert.ToDouble("-" + tempResult.ToString());
}
else
{
return tempResult;
}
}
else
{
if (pointLastLength > savelength)
{
int tempsavePointIndex = tempPointIndex + savelength + 1;//保留位数后一位的值
int tempLastData = Convert.ToInt32(valuedata.ToString().Substring(tempsavePointIndex, 1));//截取保留位那位数字
if (tempLastData >= 5)
{
//保留位那位数字加1
int tempChangeData = Convert.ToInt32(valuedata.ToString().Substring(tempPointIndex + savelength, 1)) + 1;
//截取保留位在内的总长度-1
string tempValuedata = valuedata.ToString().Substring(0, tempPointIndex + savelength);
string resultdata = tempValuedata + tempChangeData.ToString();
valuedata = Convert.ToDouble(resultdata);//现将他们用字符串拼接,再次组合成double类型的数据
if (isNegative)
{
return Convert.ToDouble("-" + valuedata.ToString());
}
else
{
return valuedata;
}
}
else
{
//保留位那位数字加1
int tempChangeData = Convert.ToInt32(valuedata.ToString().Substring(tempPointIndex + savelength, 1));
//截取保留位在内的总长度-1
string tempValuedata = valuedata.ToString().Substring(0, tempPointIndex + savelength);
string resultdata = tempValuedata + tempChangeData.ToString();
valuedata = Convert.ToDouble(resultdata);//现将他们用字符串拼接,再次组合成int类型的数据
if (isNegative)
{
return Convert.ToDouble("-" + valuedata.ToString());
}
else
{
return valuedata;
}
}
}
else
{
return valuedata;
}
}
#endregion
}
else
{
if (isNegative)
{
return Convert.ToDouble("-" + valuedata.ToString());
}
else
{
return valuedata;
}
}
#endregion
}
}
}