这是我引用的常量类。 下面是尝试使用引用的类。
以下是即将收到的错误消息。
ConstantClass的Type初始值设定项引发了异常---> System.OverflowException:对于十进制,值太大或太小。
有关它的任何想法?
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);
}
}
}
答案 0 :(得分:3)
你的问题在这里:
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。
答案 1 :(得分:0)
也许这有助于您了解自己在做什么,因此您可以决定如何解决它。我会把简短的案例作为研究的简单案例,但对于其他所有案例都可以这样说。
从你的代码中: 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,它是为登录短路保留的(在ushort中你可以使用全部16位,因为没有为标志保留位)。
你甚至可以压制演员和未选中,只需这样做:
public static int positivePastLimitShort = 32768;
因为int内部使用32位,1表示符号,31表示数据。
这是有效的,因为你有一个可以存储更大数字的值类型。
因此,使用int来传递限制的short,使用long来通过限制的int,等等。问题是处理通过较大容量类型限制的数字,如long,double或decimal。对于这些情况,您需要一种不同的方式。