Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

community creator
clojure

What is the float method in Clojure number?

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

What is the float method?

The float method is a number test method, and it is used to test if a number is a float. This means that this method checks the number to verify that it is a fraction.

Syntax

(float? number)
Syntax for float method

Parameter

As illustrated in the syntax section, the float method accepts just one parameter, that is, the number it will be testing.

Return value

The float method returns true if the number is a float and false if the number is not a float.

Usage

There are times when we want to ensure that our calculations only include numbers with decimals. The float method is very useful in such a situation. Let's see an example for understanding this better.

Example

(ns clojure.examples.hello
(:gen-class))
(defn floatt []
(def x (float? -1.9))
(println x)
(def x (float? -1))
(println x)
(def x (float? 9))
(println x))
(floatt)
An example of the float method

Code explanation

  • Line 5: We define a function float.
  • Line 6: We pass -1.9 into the float method. 
  • Line 7: We print the output and notice that the output we get is true because -1.9 is a fraction.
  • Line 9: We pass -1 into the float method. 
  • Line 10: We print the output and notice that the output we get is false because -1 is not a float.
  • Line 12: We pass 9 into the float method.
  • Line 13: We print the output and notice that the output we get is false because 9 is not a float.
  • Line 14: We call our float function to execute the code.

RELATED TAGS

community creator
clojure

CONTRIBUTOR

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring