如果第一個數字等於第二個數字,則使用compare()方法返回0;如果第一個數字大於第二個數字,則返回1;如果第一個數字小於第二個數字,則返回-1。
函數定義: (First_Number).compare(Second_Number)
返回類型: It returns 0 if the first number is equal to the second number, 1 if the first number is greater then the second number and finally -1 if the first number is less than the second number.
範例1:
// Scala program of Float compare()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Applying compare method
val result = (5).compare(5)
// Displays output
println(result)
}
}
輸出:
0
範例2:
// Scala program of Float compare()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Applying compare method
val result = (5).compare(2)
// Displays output
println(result)
}
}
輸出:
1
範例3:
// Scala program of Float compare()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Applying compare method
val result = (5).compare(9)
// Displays output
println(result)
}
}
輸出:
-1
相關用法
- Scala Float until(end: Float, step: Float)用法及代碼示例
- Scala Float to(end: Float, step: Float)用法及代碼示例
- Scala Int compare()用法及代碼示例
- Scala Int Compare用法及代碼示例
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 Scala Float compare() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。