带有示例的 Scala Float compareTo() 方法
compareTo()方法用于如果第一个数字等于第二个数字,则返回 0,如果第一个数字大于第二个数字,则返回 1,最后,如果第一个数字小于第二个数字,则返回 -1。
Method Definition: (First_Number).compareTo(Second_Number)
Return Type: 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 compareTo()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating first float number
val m1 = 3.4
// Applying compareTo method
val result = m1.compareTo(3.4)
// Displays output
println(result)
}
}
输出:
0
示例 #2:
// Scala program of Float compareTo()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating first float number
val m1 = 3.4
// Applying compareTo method
val result = m1.compareTo(2.4)
// Displays output
println(result)
}
}
输出:
1
示例#3:
// Scala program of Float compareTo()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating first float number
val m1 = 3.4
// Applying compareTo method
val result = m1.compareTo(9.5)
// Displays output
println(result)
}
}
输出:
-1