Ex 10.0 = 10 10.3 = 10.3 10.7 = 10. 7 從 Kotlin 標準庫中尋找方便的方法
uj5u.com熱心網友回復:
你可以試試這個:
double number = 23.471;
if (number % 1 != 0)
{
//round off here
System.out.print ("Decimal");
}
else
{
System.out.print ("Integer");
}
uj5u.com熱心網友回復:
如果你想得到一個字串,最簡單的方法是使用像num.toString().replace(".0",""). 對于數字轉換沒有意義,因為不同輸入的結果型別不同。
uj5u.com熱心網友回復:
您可以使用以下功能:
fun removeTrailingZeros(num: String): String {
if(!num.contains('.')) // Return the original number if it doesn't contain decimal
return num
return num
.dropLastWhile { it == '0' } // Remove trailing zero
.dropLastWhile { it == '.' } // Remove decimal in case it's the last character remaining
}
您可以在此處驗證代碼
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/336845.html
標籤:科特林
