我引用了一个常量 class 静态变量,但是当我这样做时,值会以某种方式改变

Im referencing a constant class static variables, but when i do the values somehow change

这是我引用的常量 class。 下面是试图使用引用的 class。

这是我收到的错误消息。

ConstantClass 的类型初始值设定项引发异常 ---> System.OverflowException:对于 Decimal,值太大或太小。

有什么想法吗?

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UnitTests
{
    [TestClass]

    public static class ConstantClass
    {
        public static int positiveInt = 10;
        public static int negativeInt = -10;
        public static long positivePastLimitInt = unchecked((int)2147483648);
        public static long negativePastLimitInt = unchecked((int)-2147483649);
        public static int zeroInt = 0;


        public static char lowerChar = 'c';
        public static char spaceChar = ' ';
        public static char symbolChar = '@';
        public static char numberChar = '1';
        public static char upperChar = 'D';

        public static string lowerString = "hello";
        public static string upperString = "HELLO";
        public static string emptyString = "";
        public static string spaceString = " ";
        public static string tabString = "    ";
        public static string symbolString = "!^&";
        public static string nullString = null;

        public static short positiveShort = 10;
        public static short negativeShort = -10;
        public static short zeroShort = 0;
        public static int positivePastLimitShort = unchecked((short)32768);
        public static int negativePastLimitShort = unchecked((short)-32769);

        public static long positiveLong = 10;
        public static long negativeLong = -10;
        public static long zeroLong = 0;
        public static Int64 positivePastLimitLong = (long)2147483648;
        public static Int64 negativePastLimitLong = (long)-2147483649;

        public static double positiveDouble = 10.0;
        public static double negativeDouble = -10.0;
        public static double zeroDouble = 0.0;
        public static double positiveLimitDouble = double.MaxValue;
        public static double negativeLimitDouble = double.MinValue;

        public static float positiveFloat = 10.0F;
        public static float negativeFloat = -10.0F;
        public static float zeroFloat = 0.0F;
        public static double positivePastLimitFloat = (float)float.MaxValue + 1;
        public static double negativePastLimitFloat = (float)float.MinValue - 1;

        public static bool positiveBool = true;
        public static bool negativeBool = false;


        //Here is the variable im trying to use.
        public static decimal positiveDecimal = 10.0m;
        public static decimal negativeDecimal = -10.0m;
        public static decimal zeroDecimal = 0.0m;
        public static decimal positivePastLimitDecimal = Convert.ToDecimal(80000000000000E+40);
        public static decimal negativePastLimitDecimal = Convert.ToDecimal(-8000000000000E-40);
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTests
{
    [TestClass]
    public class ByteDecimalTests
    {
        /// <summary>
        /// 
        /// </summary>
        [TestMethod]
        public void DecimalToBytes_WhenDecimalIsPositive()
        {
            //This is where i reference the constant class
            Decimal positiveDecimal = ConstantClass.positiveDecimal;
            //Decimal positiveDecimal = 12.98m;
            String positiveDecimalString = positiveDecimal.ToString();

            byte[] positiveDecimalArray = Encoding.ASCII.GetBytes(positiveDecimalString);
            byte[] array = ByteDecimal.DecimalToBytes(positiveDecimal);

            System.Diagnostics.Debug.WriteLine(Encoding.Default.GetString(positiveDecimalArray));
            System.Diagnostics.Debug.WriteLine(Encoding.Default.GetString(array));
            Assert.AreEqual(array, positiveDecimalArray);

        }
   }
}

您的问题在这里:

 public static decimal positivePastLimitDecimal = Convert.ToDecimal(80000000000000E+40)
 public static decimal negativePastLimitDecimal = Convert.ToDecimal(-8000000000000E-40);

你不能用小数来存储这么大的数字。为此使用 float 或 double。

您可以存储的maximum value小数为:79,228,162,514,264,337,593,543,950,335。

也许这可以帮助您了解自己在做什么,这样您就可以决定如何解决它。我将把简短的案例作为更容易研究的案例,但对于其他所有案例也可以这样说。

根据您的代码: public static int positivePastLimitShort = unchecked((short)32768);

短整型在内部存储为 16 位值,15 位用于数据,1 位用于符号。

符号 b14 b13 b12 b11 b10 b09 b08 b07 b06 b05 b04 b03 b02 b01 b00

当您尝试使用需要超过 15 位来表示的值时,将发生溢出。 32768 的二进制形式是 1000000000000000 但是你不能使用 bit15,它是为符号保留的 shorts(简而言之,你可以使用所有 16 位,因为没有位是为符号保留的)。

您甚至可以取消强制转换和未选中,然后执行以下操作:

public static int positivePastLimitShort = 32768;

因为 int 在内部使用 32 位,1 位用于符号,31 位用于数据。

这是有效的,因为您有一个可以存储更大数字的值类型。

因此,对超过限制的短裤使用整数,对超过限制的整数使用长整数,依此类推。问题是处理超过更大容量类型限制的数字,如 long、double 或 decimal。对于这些情况,您需要一种不同的方式。