我现在一个做一个项目,用户提出了一个奇怪的要求,即四舍六入。
具体描述如下:一般情况下,如果要求保留两位小数,程序为自动采用四舍五入的方法。
现在问题是用户要求采用四舍六入方法进行进位。
有什么好的解决方法,各位达人请不吝指教,小弟不胜感激
具体描述如下:一般情况下,如果要求保留两位小数,程序为自动采用四舍五入的方法。
现在问题是用户要求采用四舍六入方法进行进位。
有什么好的解决方法,各位达人请不吝指教,小弟不胜感激
你用Round看看是不是你要的
double a=4.6;
int b=(int)(a+0.4);
b就是5舍六入后的值
int aaaa = (int)(ccc+0.5);
int bbbb = (int)(ccc + 1.5);
ls请看看bbbb是什么值,应该是7,确是6。
我说了 不管什么值都加0.4再int 那么对于
int bbbb = (int)(ccc + 1.5);
应该是
int bbbb = (int)(ccc + 1.5+0.4);
string myPrice = String.Format("{0:N2}", Math.Round((Convert.ToDouble(price)*Convert.ToDouble(discount)*0.1),2));
其实Math.Round就是五舍六入的
Console.WriteLine(Math.Round(11.4).ToString());=11
Console.WriteLine(Math.Round(11.5).ToString());=12
Console.WriteLine(Math.Round(11.6).ToString());=12
Console.WriteLine(Math.Round(10.5).ToString());=10也就是说当是5的时候将取最接近的偶数。
2.0才有四舍六入五成双这样的例子.1.0中我试了下没有.// This example demonstrates the Math.Round() method in conjunction
// with the MidpointRounding enumeration.
using System;class Sample
{
public static void Main()
{
decimal result = 0.0m;
decimal posValue = 3.45m;
decimal negValue = -3.45m;// By default, round a positive and a negative value to the nearest even number.
// The precision of the result is 1 decimal place. result = Math.Round(posValue, 1);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, posValue);
result = Math.Round(negValue, 1);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, negValue);
Console.WriteLine();// Round a positive value to the nearest even number, then to the nearest number away from zero.
// The precision of the result is 1 decimal place. result = Math.Round(posValue, 1, MidpointRounding.ToEven);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", result, posValue);
result = Math.Round(posValue, 1, MidpointRounding.AwayFromZero);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", result, posValue);
Console.WriteLine();// Round a negative value to the nearest even number, then to the nearest number away from zero.
// The precision of the result is 1 decimal place. result = Math.Round(negValue, 1, MidpointRounding.ToEven);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", result, negValue);
result = Math.Round(negValue, 1, MidpointRounding.AwayFromZero);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", result, negValue);
Console.WriteLine();
}
}
/*
This code example produces the following results: 3.4 = Math.Round( 3.45, 1)
-3.4 = Math.Round(-3.45, 1) 3.4 = Math.Round( 3.45, 1, MidpointRounding.ToEven)
3.5 = Math.Round( 3.45, 1, MidpointRounding.AwayFromZero)-3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven)
-3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero)*/
四舍五入 与 四舍六入五成双 C#,vs.net.j#,vbscript都是采用的四舍六入五成双
SQL server 使用的是四舍五入double aa = 1.25;
aa = Math.Round(aa,1);
Response.Write(aa.ToString());
返回的是1.2SQL 中 select round(1.25,1) 返回的是1.3
/// 转化
/// </summary>
/// <param name="OrignNum">要转换的数值</param>
/// <param name="n">位数</param>
/// <returns>转换后的字符串</returns>
protected string ZH(double OrignNum,int n)
{
string[] str=OrignNum.ToString().Split(new char[]{'.'});
if(str.Length<1 || str.Length>2)
{
return Math.PI.ToString();// 你可以替换为你认为不可能的数值
}
char[] splitNum=str[1].ToCharArray();
if(n>=splitNum.Length)
{
return OrignNum.ToString();
}
int SplitW=int.Parse(splitNum[n].ToString());
if(SplitW>=6)
{
int v=str[1].Substring(0,n).Length;
str[1]=(int.Parse(str[1].Substring(0,n))+1).ToString();
if(v<str[1].Length)
{
str[1]=str[1].Substring(1);
str[0]=(int.Parse(str[0])+1).ToString();
}
}
else
{
str[1]=str[1].Substring(0,n);
}
return str[0]+"."+str[1];
}
代码粗略验,如有bug请指正