如何将小数四舍五入到小数点后两位

发布于 2024-12-13 22:34:38 字数 260 浏览 3 评论 0原文

我有 2 个十进制数:

1999,9999
1999,99

如果我使用函数

decimal.Round(Temp, 2);

,那么我会得到以下结果:

2000,00
1999,99

如何确保即使有 1999,999999 也会四舍五入到 1999,99 而不是 2000,00。

谢谢。

I have 2 decimal numbers:

1999,9999
1999,99

if I use function

decimal.Round(Temp, 2);

then I have these results:

2000,00
1999,99

How to make sure that even if there is 1999,999999 it will round to 1999,99 instead of 2000,00.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蓝礼 2024-12-20 22:34:38

四舍五入总是会使 1999,999 移动到 2000。听起来你想截断。

您可以通过一些乘法/除法来做到这一点:

decimal TruncateToPlaces(decimal value, int places)
{
    decimal multiplier = Math.Pow(10m, places);
    return decimal.Truncate(value * multiplier) / multiplier;
}

然后您可以执行以下操作:

decimal value = TruncateToPlaces(1999.9999, 2);

Rounding will always make 1999,999 move to 2000. It sounds like you want to truncate.

You can do this via some multiplication/division:

decimal TruncateToPlaces(decimal value, int places)
{
    decimal multiplier = Math.Pow(10m, places);
    return decimal.Truncate(value * multiplier) / multiplier;
}

You can then do:

decimal value = TruncateToPlaces(1999.9999, 2);
心房敞 2024-12-20 22:34:38

您需要截断数字。一种方法是:decimal.Truncate(* 100) / 100;

You need to truncate digits. One way to do it is: decimal.Truncate(<number> * 100) / 100;

国粹 2024-12-20 22:34:38
decimal d = 1999.999999; //or any

int temp = d*100; //199999
decimal result = temp/(decimal)100; //199.99
decimal d = 1999.999999; //or any

int temp = d*100; //199999
decimal result = temp/(decimal)100; //199.99
与之呼应 2024-12-20 22:34:38

那么简短的答案是......1999,9999四舍五入到小数点后两位实际上是2000,00

有多种不同的舍入策略 - http://en.wikipedia.org/wiki/Rounding — 您可能想阅读它们。

但是,如果您不想舍入该值而是想截断它,那么这应该可以满足您的要求。 scale 是 10 的幂,指示应发生截断的位置。如果scale 是10 的负幂(例如,10-2),则将截断小数点右侧的许多位。如果 scale 为非负数,则截断发生在小数点左侧(例如,1 aka 10 0 将截断分数,100(或 102)的 scale 将截断 100 位数右侧的所有内容,将 。

decimal TruncateToHundredthsPlace( decimal value )
{
  const decimal scale = 0.01m ; // 10^-2
  decimal result = value - ( value % 0.01m ) ;
  return results ;
}

或者,您可以编写如下代码所示的方法:它适用于任何比例:比例 的负值会截断该比例 小数点右侧的许多位置;非零值会截断小数点左侧的

public static decimal Truncate( decimal value , int scale )
{
    Decimal factor = Power(10,scale) ;
    decimal result = value - ( value % factor ) ;

    return result ;
}
private static decimal Power( int m , int n )
{
    if ( m < 1 ) throw new ArgumentOutOfRangeException("m") ;

    Decimal @base  = (decimal) m ;
    Decimal factor = 1m ; // m^0 = 1
    while ( n > 0 )
    {
        factor *= @base ;
        --n ;
    }
    while ( n < 0 )
    {
        factor /= @base ;
        ++n ;
    }
    return factor ;
}

10 的幂,但是,两个简单的优化可以加快速度:

  • 首先,预先计算一些合理的数字。静态查找表中 10 的幂,例如 10-10–10+10,我将创建一个带有 a 的 decimal[] 。负下限:则检查指定的比例是否在查找表中仅包括检查比例值是否是数组的有效索引。如果是,则使用比例值作为索引提取因子。

  • 第二个优化是在计算 10 个值时缓存它们。如果您已经计算了该值,则实际上不需要重新计算它,只需使用比例作为键从缓存中获取它即可。

Well the short answer is that....1999,9999 rounded to 2 decimal places is in fact 2000,00.

There are a number of different rounding strategies — http://en.wikipedia.org/wiki/Rounding — you might want to read up on them.

But if you don't want to round the value and instead want to truncate it, then this should do you. scale is a power of 10 indicating the position at which truncation should occur. If scale is a negative power of 10 (e.g., 10-2), then truncation will occur that many digits to the right of the decimal point. If scale is non-negative, then truncation occurs to the left of the decimal point (e.g., a scale of 1 aka 100 will truncate the fraction, and a scale of 100 (or 102) will truncate everything to the right of the 100s place, converting 1999.9999m into 1900m.

decimal TruncateToHundredthsPlace( decimal value )
{
  const decimal scale = 0.01m ; // 10^-2
  decimal result = value - ( value % 0.01m ) ;
  return results ;
}

Alternatively, you could write a method like the following code. It works for any scale: negative values for scale truncate that many positions right of the decimal point; non-zero values truncate left of the decimal point.

public static decimal Truncate( decimal value , int scale )
{
    Decimal factor = Power(10,scale) ;
    decimal result = value - ( value % factor ) ;

    return result ;
}
private static decimal Power( int m , int n )
{
    if ( m < 1 ) throw new ArgumentOutOfRangeException("m") ;

    Decimal @base  = (decimal) m ;
    Decimal factor = 1m ; // m^0 = 1
    while ( n > 0 )
    {
        factor *= @base ;
        --n ;
    }
    while ( n < 0 )
    {
        factor /= @base ;
        ++n ;
    }
    return factor ;
}

Computing a power of 10 in decimal is expensive. Two simple optimization would speed it up, though:

  • First, precompute some reasonable number of powers of 10 in a static lookup table, say 10-10–10+10 inclusive. I'd create a decimal[] with a negative lower bound: then the check to see if the specified scale is in the lookup table just consists of checking to see if the scale value is a valid index into the array. If so, pull the factor out using the scale value as the index.

  • A second optimization would be to cache power of 10 values as you compute them. If you've already computed the value, you don't really need to recompute it, just fish it from cache using the scale as the key.

绾颜 2024-12-20 22:34:38

试试这个:

Convert.ToDecimal(x.ToString("#.##"));

Try this:

Convert.ToDecimal(x.ToString("#.##"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文