Open In App

Scala Float compare() method with example

Last Updated : 15 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The compare() method is utilized to returns 0 if the first number is equal to the second number, 1 if the first number is greater than the second number and finally -1 if the first number is less than the second number.

Method Definition: (First_Number).compare(Second_Number) Return Type: It returns 0 if the first number is equal to the second number, 1 if the first number is greater than the second number and finally -1 if the first number is less than the second number.

Example #1: 

Scala




// 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)
     
    }
}


Output:

0

Example #2: 

Scala




// 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)
     
    }
}


Output:

1

Example #3: 

Scala




// 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)
     
    }
}


Output:

-1


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads