在c#中验证十进制,以便在SQL Server中存储

时间:2022-09-23 20:02:48

I have a decimal database column decimal (26,6).

我有一个十进制数据库列decimal (26,6)

As far as I can gather this means a precision of 26 and a scale of 6.

据我所知,这意味着精度为26,刻度为6。

I think this means that the number can be a total of 26 digits in length and 6 of these digits can be after the decimal place.

我认为这意味着这个数字可以是总共26个数字的长度,其中6个数字可以在小数点后。

In my WPF / C# frontend I need to validate an incoming decimal so that I can be sure that it can be stored in SQL Server without truncation etc.

在我的WPF / c#前端中,我需要验证传入的小数,这样我就可以确保它可以存储在SQL服务器中,而不会被截断等等。

So my question is there a way to check that decimal has a particular precision and scale.

我的问题是是否有一种方法来检验小数的精度和比例。

Also as an aside I have heard that SQL Server stores decimal in a completely different way to the CLR, is this true and if so is it something I need to worry about?

另外,我听说SQL Server以完全不同的方式存储decimal到CLR,这是真的吗?

2 个解决方案

#1


4  

straight forward way to determine if a given precision,scale of decimal number is greater than 26,6 would be to check the length of its string equivalent.

直接向前的方法来确定一个给定的精度,十进制数的比例是否大于26,6是检查它的字符串的长度是否相等。

    public static bool WillItTruncate(double dNumber, int precision, int scale) {
        string[] dString = dNumber.ToString("#.#", CultureInfo.InvariantCulture).Split('.');
        return (dString[0].Length > (precision - scale) || dString.Length>1?dString[1].Length > scale:true);
    }

The maximum precision for C# decimal datatype seems to be 29 digits whereas SQL decimal can have 38 digits. So you may not be hitting the maximum value of SQL decimal from C#.

c# decimal数据类型的最大精度似乎是29位,而SQL decimal可以有38位数字。因此,您可能没有达到c#中SQL decimal的最大值。

#2


1  

If you already know destination scale and precision of decimal type at compile time, do simple comparison. For decimal(13,5):

如果您在编译时已经知道目标比例和十进制类型的精度,请进行简单的比较。小数(13、5):

public static bool IsValidDecimal13_5(decimal value)
{
    return -99999999.99999M <= value && value <= 99999999.99999M;
} 

#1


4  

straight forward way to determine if a given precision,scale of decimal number is greater than 26,6 would be to check the length of its string equivalent.

直接向前的方法来确定一个给定的精度,十进制数的比例是否大于26,6是检查它的字符串的长度是否相等。

    public static bool WillItTruncate(double dNumber, int precision, int scale) {
        string[] dString = dNumber.ToString("#.#", CultureInfo.InvariantCulture).Split('.');
        return (dString[0].Length > (precision - scale) || dString.Length>1?dString[1].Length > scale:true);
    }

The maximum precision for C# decimal datatype seems to be 29 digits whereas SQL decimal can have 38 digits. So you may not be hitting the maximum value of SQL decimal from C#.

c# decimal数据类型的最大精度似乎是29位,而SQL decimal可以有38位数字。因此,您可能没有达到c#中SQL decimal的最大值。

#2


1  

If you already know destination scale and precision of decimal type at compile time, do simple comparison. For decimal(13,5):

如果您在编译时已经知道目标比例和十进制类型的精度,请进行简单的比较。小数(13、5):

public static bool IsValidDecimal13_5(decimal value)
{
    return -99999999.99999M <= value && value <= 99999999.99999M;
}