在 C# 中,可以使用以下方法来判断一个数字是否为'decimal(4,2)'或两位整数:

  1. 使用正则表达式进行匹配:
string number = '12.34'; // 需要判断的数字
bool isDecimal = Regex.IsMatch(number, @'^\d{1,2}(\.\d{1,2})?$');

这个正则表达式的意思是匹配1到2位整数,可以带有小数点和1到2位小数。

  1. 使用 TryParse 方法进行解析:
string number = '12.34'; // 需要判断的数字
decimal parsedNumber;
bool isDecimal = decimal.TryParse(number, out parsedNumber) && parsedNumber >= 0 && parsedNumber <= 99.99m;

这种方法会尝试将字符串解析为 decimal 类型,并判断解析结果是否在 0 到 99.99 之间。

无论使用哪种方法,最终判断结果都会保存在布尔值 isDecimal 中,为 true 表示是'decimal(4,2)'或两位整数,为 false 表示不是。

C# 判断数字是否为 decimal(4,2) 或两位整数