gpt4 book ai didi

.net - 计算 System.Decimal 精度和小数位数

转载 作者:行者123 更新时间:2023-12-03 11:05:26 25 4
gpt4 key购买 nike

假设我们有一个 System.Decimal 数。

为了说明,让我们举一个其 ToString() 表示如下:

d.ToString() = "123.4500"

关于这个十进制可以说如下。出于我们的目的,比例定义为小数点右侧的位数。有效比例类似,但忽略小数部分中出现的任何尾随零。 (换句话说,这些参数的定义类似于 SQL 小数加上一些附加参数,以说明小数部分中尾随零的 System.Decimal 概念。)
  • 精度:7
  • 规模:4
  • 有效精度:5
  • 有效量表:2

  • 给定一个任意 System.Decimal,如何有效地计算所有这四个参数而不转换为字符串并检查字符串?该解决方案可能需要 Decimal.GetBits。

    还有一些例子:
    Examples Precision  Scale  EffectivePrecision  EffectiveScale
    0 1 (?) 0 1 (?) 0
    0.0 2 (?) 1 1 (?) 0
    12.45 4 2 4 2
    12.4500 6 4 4 2
    770 3 0 3 0

    (?) 或者将这些精度解释为零也可以。

    最佳答案

    是的,您需要使用 Decimal.GetBits .不幸的是,您必须使用 96 位整数,而 .NET 中没有处理 96 位的简单整数类型。另一方面,您可以使用 Decimal本身...

    这是一些生成与您的示例相同的数字的代码。希望你觉得它有用 :)

    using System;

    public class Test
    {
    static public void Main(string[] x)
    {
    ShowInfo(123.4500m);
    ShowInfo(0m);
    ShowInfo(0.0m);
    ShowInfo(12.45m);
    ShowInfo(12.4500m);
    ShowInfo(770m);
    }

    static void ShowInfo(decimal dec)
    {
    // We want the integer parts as uint
    // C# doesn't permit int[] to uint[] conversion,
    // but .NET does. This is somewhat evil...
    uint[] bits = (uint[])(object)decimal.GetBits(dec);


    decimal mantissa =
    (bits[2] * 4294967296m * 4294967296m) +
    (bits[1] * 4294967296m) +
    bits[0];

    uint scale = (bits[3] >> 16) & 31;

    // Precision: number of times we can divide
    // by 10 before we get to 0
    uint precision = 0;
    if (dec != 0m)
    {
    for (decimal tmp = mantissa; tmp >= 1; tmp /= 10)
    {
    precision++;
    }
    }
    else
    {
    // Handle zero differently. It's odd.
    precision = scale + 1;
    }

    uint trailingZeros = 0;
    for (decimal tmp = mantissa;
    tmp % 10m == 0 && trailingZeros < scale;
    tmp /= 10)
    {
    trailingZeros++;
    }

    Console.WriteLine("Example: {0}", dec);
    Console.WriteLine("Precision: {0}", precision);
    Console.WriteLine("Scale: {0}", scale);
    Console.WriteLine("EffectivePrecision: {0}",
    precision - trailingZeros);
    Console.WriteLine("EffectiveScale: {0}", scale - trailingZeros);
    Console.WriteLine();
    }
    }

    关于.net - 计算 System.Decimal 精度和小数位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/763942/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com