Related Articles
Scala Float compareTo() method with example
• Last Updated : 29 Oct, 2019

The compareTo() method is utilized to 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.

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.

Example #1:

 `// Scala program of Float compareTo()``// method`` ` `// Creating object``object` `GfG``{ `` ` `    ``// Main method``    ``def` `main(args``:``Array[String])``    ``{``     ` `        ``// Creating first float number ``        ``val` `m``1` `=` `3.4``         ` `        ``// Applying compareTo method``        ``val` `result ``=` `m``1``.compareTo(``3.4``)``         ` `        ``// Displays output``        ``println(result)``     ` `    ``}``} `
Output:

```0
```

Example #2:

 `// Scala program of Float compareTo()``// method`` ` `// Creating object``object` `GfG``{ `` ` `    ``// Main method``    ``def` `main(args``:``Array[String])``    ``{``     ` `        ``// Creating first float number ``        ``val` `m``1` `=` `3.4``         ` `        ``// Applying compareTo method``        ``val` `result ``=` `m``1``.compareTo(``2.4``)``         ` `        ``// Displays output``        ``println(result)``     ` `    ``}``} `
Output:
```1
```

Example #3:

 `// Scala program of Float compareTo()``// method`` ` `// Creating object``object` `GfG``{ `` ` `    ``// Main method``    ``def` `main(args``:``Array[String])``    ``{``     ` `        ``// Creating first float number ``        ``val` `m``1` `=` `3.4``         ` `        ``// Applying compareTo method``        ``val` `result ``=` `m``1``.compareTo(``9.5``)``         ` `        ``// Displays output``        ``println(result)``     ` `    ``}``} `
Output:
```-1
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up