一个double型的数字取n位小数,其它小数位全部舍去 double d=12.3424, d1;d1 = d - 0.005;d=double.Parse(d1.ToString("F2")); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 try double d = 12.3424; string st = d.ToString(); st = st.Substring(0,st.LastIndexOf(".")+3); d = double.Parse(st); double a = 3212.09887; string bx = ""; string[] temp = a.ToString().Split('.'); bx = temp[0] + "."; for(int i = 0;i<2;i++) { bx += temp[1][i]; } Console.WriteLine(bx); double d = 12.12999;string t = d.ToString();t = t.Substring(0,t.IndexOf(".")+3);d = Double.Parse(t); 格式化数字提供的方法一般都是四舍五入,因此可以自己写一个方法,把它从数字转换成字符串,截取以后再转换成数字。这样也挺简单的,不要企图希望.NET Framework为你做好一切工作,呵呵 在d.ToString()的时候有些数字就不是原来的数字了,例如上面提到的828.99999999999989,ToString后变成829,这当然不是我想要的结果啊。就是因为这个原因我才放弃了使用我以前写的这个函数:public double GetTrun(double myDouble,int Length) { string tempString; tempString=myDouble.ToString (); int startInt=tempString.IndexOf ("."); if (startInt==-1) { //r如果是整数,直接返回 return myDouble; } int tempint=tempString.Substring (startInt+1).Length ;//取小数点后共有几位 if (tempint<=Length) {//如果小数点后的位数小于等于您要的精度数,则直接返回 return myDouble; } tempString=tempString.Substring(0,startInt+1+Length); myDouble=Convert.ToDouble (tempString); return myDouble; } "828.99999999999989" ? 这个double太“长”了,当然就不能采用上面的方式了,因为: 调用Double.ToString()默认情况下,返回值只包含 15 位精度,但内部维护的最大精度是 17 位。如果此实例的值超过 15 位,则 ToString 返回 PositiveInfinitySymbol 或 NegativeInfinitySymbol,而不是预期的数字。如果要求更高的精度,请用“G17”或“R”格式规范指定 format;前一种格式总是返回 17 位精度;而后一种格式在数字可以用 15 位精度表示时返回 15 位精度,在数字只能用最大精度表示时返回 17 位精度。 double d=12345.678902保留2位:(double)((int)(d*100))/100n位也可以同样做啊 Math.Round 方法 [C#]请参见Math 类 | Math 成员 | System 命名空间 | C++ 托管扩展编程 语言C#C++JScriptVisual Basic全部显示返回最接近指定值的数字。重载列表返回最接近指定值的整数。受 .NET Framework 精简版的支持。[Visual Basic] Overloads Public Shared Function Round(Decimal) As Decimal[C#] public static decimal Round(decimal);[C++] public: static Decimal Round(Decimal);[JScript] public static function Round(Decimal) : Decimal;返回最接近指定值的整数。受 .NET Framework 精简版的支持。[Visual Basic] Overloads Public Shared Function Round(Double) As Double[C#] public static double Round(double);[C++] public: static double Round(double);[JScript] public static function Round(double) : double;返回具有指定精度、最接近指定值的数。受 .NET Framework 精简版的支持。[Visual Basic] Overloads Public Shared Function Round(Decimal, Integer) As Decimal[C#] public static decimal Round(decimal, int);[C++] public: static Decimal Round(Decimal, int);[JScript] public static function Round(Decimal, int) : Decimal;返回具有指定精度、最接近指定值的数。受 .NET Framework 精简版的支持。[Visual Basic] Overloads Public Shared Function Round(Double, Integer) As Double[C#] public static double Round(double, int);[C++] public: static double Round(double, int);[JScript] public static function Round(double, int) : double;示例下面的代码演示就近舍入。[Visual Basic] Math.Round(3.44, 1) 'Returns 3.4.Math.Round(3.45, 1) 'Returns 3.4.Math.Round(3.46, 1) 'Returns 3.5.[C#] Math.Round(3.44, 1); //Returns 3.4.Math.Round(3.45, 1); //Returns 3.4.Math.Round(3.46, 1); //Returns 3.5. 其实, 828.99999999999989这个数字是8.29*100的结果,还有类似8.2*100,8.12*100,计算的结果并不是整数,都是类似上面这个数字的数,应该还有其他的数我记不清更多了。我也曾经用下面这个函数想达到我得要求:public double GetTrun(double myDouble,int Length){ int i; double y=1; for(i=0;i<Length;i++) { y*=10;} myDouble=Math.Floor (myDouble*y)/y; return myDouble; }但就是因为上面相乘的结果不对而出错了。同时也要考虑如果真的传入值就是8.2899999999999989数字,结果又会怎样?我觉得:这个方法的关键在8.29*100<>829,而第一种方法的关键在ToString时考虑精度的问题。如果采用精度的话,828.99999999999989精度17可以,我查了一下,double类型的精度在-1.79769313486232e308和+1.79769313486232e308之间,那么如果下一个数的精度变大了,我又该将"G17"改成多少?或者取它的最大精度308?这样double类型的数字是否浪费了太多的地址空间? 去掉尾数,而不是四舍五入的函数:public double GetTrun(double myDouble,int Length){ double rate = 0.5; for(int i = 0; i < Length; i++) { rate /= 10; } double newdouble = myDouble - rate; return newdouble;} 另外说明一下,double型的数据是没有小数点位数的,他总是保持固定的精度。我上面这个函数只是获得一个新的double数据,可以让你直接 Doulbe.ToString()而已。 你所需要的只是控制显示格式,double的内部精度是不能更改的。 乘100,转换为int,再转换为double,除以100即可:public double formatNum( double num ){ return (double)((int)(num*100))/100; } 多线程的异步调用问题 c#中ListView如何搜索某个子项,谢谢!! c#文件分割合并的问题 求一个绑定的表达式 谁有MySQLDriverCS的文件? C#中递归的问题!!请高手指教 C#如何格式化货币 DataGrid,这个东西真麻烦????? C#必败(之一)!!!!!!!!!!! 关于随机数的边界问题Random1.Next(0, len)这个0可以取到吗 200分问题,对于菜鸟的我超难! Access数据库如何批量操作呀 ?
try double d = 12.3424;
string st = d.ToString();
st = st.Substring(0,st.LastIndexOf(".")+3);
d = double.Parse(st);
string bx = "";
string[] temp = a.ToString().Split('.');
bx = temp[0] + ".";
for(int i = 0;i<2;i++)
{
bx += temp[1][i];
}
Console.WriteLine(bx);
string t = d.ToString();
t = t.Substring(0,t.IndexOf(".")+3);d = Double.Parse(t);
public double GetTrun(double myDouble,int Length)
{ string tempString;
tempString=myDouble.ToString ();
int startInt=tempString.IndexOf (".");
if (startInt==-1)
{
//r如果是整数,直接返回
return myDouble;
}
int tempint=tempString.Substring (startInt+1).Length ;//取小数点后共有几位
if (tempint<=Length)
{//如果小数点后的位数小于等于您要的精度数,则直接返回
return myDouble;
}
tempString=tempString.Substring(0,startInt+1+Length);
myDouble=Convert.ToDouble (tempString);
return myDouble;
}
"828.99999999999989" ? 这个double太“长”了,当然就不能采用上面的方式了,因为: 调用Double.ToString()默认情况下,返回值只包含 15 位精度,但内部维护的最大精度是 17 位。如果此实例的值超过 15 位,则 ToString 返回 PositiveInfinitySymbol 或 NegativeInfinitySymbol,而不是预期的数字。如果要求更高的精度,请用“G17”或“R”格式规范指定 format;前一种格式总是返回 17 位精度;而后一种格式在数字可以用 15 位精度表示时返回 15 位精度,在数字只能用最大精度表示时返回 17 位精度。
保留2位:
(double)((int)(d*100))/100
n位也可以同样做啊
Math 类 | Math 成员 | System 命名空间 | C++ 托管扩展编程 语言
C#C++JScriptVisual Basic全部显示
返回最接近指定值的数字。
重载列表
返回最接近指定值的整数。
受 .NET Framework 精简版的支持。
[Visual Basic] Overloads Public Shared Function Round(Decimal) As Decimal
[C#] public static decimal Round(decimal);
[C++] public: static Decimal Round(Decimal);
[JScript] public static function Round(Decimal) : Decimal;
返回最接近指定值的整数。
受 .NET Framework 精简版的支持。
[Visual Basic] Overloads Public Shared Function Round(Double) As Double
[C#] public static double Round(double);
[C++] public: static double Round(double);
[JScript] public static function Round(double) : double;
返回具有指定精度、最接近指定值的数。
受 .NET Framework 精简版的支持。
[Visual Basic] Overloads Public Shared Function Round(Decimal, Integer) As Decimal
[C#] public static decimal Round(decimal, int);
[C++] public: static Decimal Round(Decimal, int);
[JScript] public static function Round(Decimal, int) : Decimal;
返回具有指定精度、最接近指定值的数。
受 .NET Framework 精简版的支持。
[Visual Basic] Overloads Public Shared Function Round(Double, Integer) As Double
[C#] public static double Round(double, int);
[C++] public: static double Round(double, int);
[JScript] public static function Round(double, int) : double;
示例
下面的代码演示就近舍入。
[Visual Basic]
Math.Round(3.44, 1) 'Returns 3.4.
Math.Round(3.45, 1) 'Returns 3.4.
Math.Round(3.46, 1) 'Returns 3.5.
[C#]
Math.Round(3.44, 1); //Returns 3.4.
Math.Round(3.45, 1); //Returns 3.4.
Math.Round(3.46, 1); //Returns 3.5.
public double GetTrun(double myDouble,int Length)
{
int i;
double y=1;
for(i=0;i<Length;i++)
{
y*=10;
}
myDouble=Math.Floor (myDouble*y)/y;
return myDouble;
}
但就是因为上面相乘的结果不对而出错了。同时也要考虑如果真的传入值就是8.2899999999999989数字,结果又会怎样?
我觉得:这个方法的关键在8.29*100<>829,而第一种方法的关键在ToString时考虑精度的问题。如果采用精度的话,828.99999999999989精度17可以,我查了一下,double类型的精度在-1.79769313486232e308和+1.79769313486232e308之间,那么如果下一个数的精度变大了,我又该将"G17"改成多少?或者取它的最大精度308?这样double类型的数字是否浪费了太多的地址空间?
{
double rate = 0.5;
for(int i = 0; i < Length; i++)
{
rate /= 10;
} double newdouble = myDouble - rate; return newdouble;
}
我上面这个函数只是获得一个新的double数据,可以让你直接 Doulbe.ToString()而已。
{
return (double)((int)(num*100))/100;
}