本文介紹了如何檢查值是否為整數類型?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我需要檢查一個值是否為整數.我發現了這個:如何檢查輸入值是否為整數還是浮點數?,但如果我沒記錯的話,即使 value 本身,變量仍然是 double
類型確實是一個整數
.
I need to check if a value is an integer. I found this: How to check whether input value is integer or float?, but if I'm not mistaken, the variable there is still of type double
even though the value itself is indeed an integer
.
推薦答案
如果輸入值可以是整數以外的數字形式,請檢查
If input value can be in numeric form other than integer , check by
if (x == (int)x)
{
// Number is integer
}
如果正在傳遞字符串值,請使用 Integer.parseInt(string_var).
請確保在轉換失敗時使用 try catch 進行錯誤處理.
If string value is being passed , use Integer.parseInt(string_var).
Please ensure error handling using try catch in case conversion fails.
這篇關于如何檢查值是否為整數類型?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!