[英]How to check if TextBox.Text is round number?
我需要檢查我的 TextBox 是否包含整數,即使是這種格式:“5.00”
我嘗試了 Integer.TryParse(),但它返回 False。 它僅在這種情況下起作用,當 TextBox 包含以下格式的數字時:“5”
將您的字符串數字轉換為Double
,然后使用Math.Round()
方法進行檢查
//Convert to Double && round double number and check with original double number
if((Double.TryParse(textValue, out double number)) && Math.Round(number) == number)
{
//Here you go;
}
您可以使用接受 NumberStyle 的Parse
的重載
var i = int.Parse("5.00", System.Globalization.NumberStyles.AllowDecimalPoint);
或TryParse
int.TryParse("5.00", System.Globalization.NumberStyles.AllowDecimalPoint, null, out int nbr);
或者:
Function IsRound(sNr As String) As Boolean
Return IsNumeric(sNr) AndAlso Decimal.Truncate(CDec(sNr)) = CDec(sNr)
End Function
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.