检查字符串是数字是十进制还是整数而不使用包含逗号或点

Check if the string is number is decimal or integer without using contains comma or dot

我正在尝试查找字符串值是否为数字,然后检查它是小数还是整数。基于它我们调用一定的逻辑

string value ="1,537"; // Value contains comma as it uses a Swedish culture sv-SE
if (decimal.TryParse(value, out var eval)) // First check if it is a number
{
    bool isWholeNumber = Math.Floor(decimal.Parse(value)) == decimal.Parse(value);
}

上述逻辑的问题是它将“1,00”视为整数并将运行视为整数逻辑

As a work around I can check if the string contains ",". But I feel its not a better way as the culture can be changed by user at any time.

是否有更好的检查方法?

OneIDE Fiddle

截至目前的输出

The value 1,000 is integer True // This is wrong
The value 1,4500 is integer False
The value 1 is integer True

在你的情况下,我会使用 NumberStyles,使用 NumberStyles.Integer 可能会让你的输入确定为唯一的数字。

decimal.TryParse(value,NumberStyles.Integer,CultureInfo.CreateSpecificCulture("sv-SE"), out var eval)

c# fiddle