gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:36:40 25 4
gpt4 key购买 nike

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

这是我收到的错误消息。

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。

关于c# - 我引用了一个常量类静态变量,但是当我这样做时,值会以某种方式改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30353908/

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