.net 截断十进制数不四舍五入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/329957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:43:12  来源:igfitidea点击:

Truncate Decimal number not Round Off

.netfloating-pointtruncate

提问by ManojN

Possible Duplicate:
c# - How do I round a decimal value to 2 decimal places (for output on a page)

可能的重复:
c# - 如何将十进制值四舍五入到 2 个小数位(用于页面上的输出)

I want to truncate the decimals like below

我想截断小数如下

i.e.

IE

  • 2.22939393 -> 2.229
  • 2.22977777 -> 2.229
  • 2.22939393 -> 2.229
  • 2.22977777 -> 2.229

采纳答案by Bill the Lizard

double d = 2.22977777;
d = ( (double) ( (int) (d * 1000.0) ) ) / 1000.0 ;

Of course, this won't work if you're trying to truncate rounding error, but it should work fine with the values you give in your examples. See the first two answers to this questionfor details on why it won't work sometimes.

当然,如果您试图截断舍入误差,这将不起作用,但它应该适用于您在示例中给出的值。有关为什么有时不起作用的详细信息,请参阅此问题的前两个答案。

回答by CMS

You can use Math.Round:

您可以使用Math.Round

decimal rounded = Math.Round(2.22939393, 3); //Returns 2.229

Or you can use ToString with the N3 numeric format.

或者您可以将 ToString 与 N3数字格式一起使用

string roundedNumber = number.ToString("N3");

EDIT:Since you don't want rounding, you can easily use Math.Truncate:

编辑:由于您不想四舍五入,您可以轻松使用Math.Truncate

Math.Truncate(2.22977777 * 1000) / 1000; //Returns 2.229

回答by Carl H?rberg

A function to truncate an arbitrary number of decimals:

截断任意小数位数的函数:

public decimal Truncate(decimal number, int digits)
{
  decimal stepper = (decimal)(Math.Pow(10.0, (double)digits));
  int temp = (int)(stepper * number);
  return (decimal)temp / stepper;
}

回答by Glenn Slayden

Here's an extension method which does not suffer from integer overflow (like some of the above answers do). It also caches some powers of 10 for efficiency.

这是一个不受整数溢出影响的扩展方法(就像上面的一些答案一样)。它还缓存了一些 10 的幂以提高效率。

static double[] pow10 = { 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10 };
public static double Truncate(this double x, int precision)
{
    if (precision < 0)
        throw new ArgumentException();
    if (precision == 0)
        return Math.Truncate(x);
    double m = precision >= pow10.Length ? Math.Pow(10, precision) : pow10[precision];
    return Math.Truncate(x * m) / m;
}

回答by Jim

This is similar to TcKs suggestion above, but using math.truncate rather than int conversions

这类似于上面的 TcKs 建议,但使用 math.truncate 而不是 int 转换

VB: but you'll get the idea

VB:但你会明白的

Private Function TruncateToDecimalPlace(byval ToTruncate as decimal, byval DecimalPlaces as integer) as double 
dim power as decimal = Math.Pow(10, decimalplaces)
return math.truncate(totruncate * power) / power
end function

回答by Pavlo Neiman

Try this

尝试这个

double d = 2.22912312515;
int demention = 3;
double truncate = Math.Truncate(d) + Math.Truncate((d - Math.Truncate(d)) * Math.Pow(10.0, demention)) / Math.Pow(10.0, demention);

回答by magicrebirth

Maybe another quick solution could be:

也许另一个快速解决方案可能是:

>>> float("%.1f" % 1.00001)
1.0
>>> float("%.3f" % 1.23001)
1.23
>>> float("%.5f" % 1.23001)
1.23001

回答by mezoid

What format are you wanting the output?

你想要什么格式的输出?

If you're happy with a string then consider the following C# code:

如果您对字符串感到满意,请考虑以下 C# 代码:

double num = 3.12345;
num.ToString("G3");

The result will be "3.12".

结果将是“3.12”。

This link might be of use if you're using .NET. http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

如果您使用的是 .NET,则此链接可能有用。 http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

I hope that helps....but unless you identify than language you are using and the format in which you want the output it is difficult to suggest an appropriate solution.

我希望这会有所帮助....但是除非您确定您使用的语言和您想要的输出格式,否则很难提出适当的解决方案。

回答by TcKs

Try this:

尝试这个:

decimal original = GetSomeDecimal(); // 22222.22939393
int number1 = (int)original; // contains only integer value of origina number
decimal temporary = original - number1; // contains only decimal value of original number
int decimalPlaces = GetDecimalPlaces(); // 3
temporary *= (Math.Pow(10, decimalPlaces)); // moves some decimal places to integer
temporary = (int)temporary; // removes all decimal places
temporary /= (Math.Pow(10, decimalPlaces)); // moves integer back to decimal places
decimal result = original + temporary; // add integer and decimal places together

It can be writen shorter, but this is more descriptive.

它可以写得更短,但这更具描述性。

EDIT:Short way:

编辑:简短的方法:

decimal original = GetSomeDecimal(); // 22222.22939393
int decimalPlaces = GetDecimalPlaces(); // 3
decimal result = ((int)original) + (((int)(original * Math.Pow(10, decimalPlaces)) / (Math.Pow(10, decimalPlaces));

回答by user274105

Forget Everything just check out this

忘记一切,看看这个

double num = 2.22939393;
num  = Convert.ToDouble(num.ToString("#0.000"));