Ruby | Float Class Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In Ruby, Float class is a subclass of Numeric class. The objects of the Float class represents real numbers using the native architecture's double- precision floating-point representation. Public Instance Methods Arithmetic Operations: This method perform various arithmetic operations on float. Addition: It returns the result of the sum of float and numeric value in floating-point number. float + numeric Subtraction: It returns the result of difference of float and numeric value in floating-point number. float - numeric Multiplication: It returns the result of product of float and numeric value in floating-point number. float * numeric Division: It returns the result of division of float and numeric value in floating-point number. float / numeric Modulo: It returns the result of the modulo of float and numeric value in floating-point number. float % numeric Exponent: It returns the result of power of float and numeric value in floating-point number. float ** numeric Unary minus: It returns floating-point number. float -@ Example: Ruby # Ruby program to illustrate # Arithmetic operation a = 2.1 b = 2 # Addition c = a + b puts "addition #{c}" # Subtraction d = a - b puts "subtraction #{d}" # Multiplication e = a * b puts "multiplication #{e}" # Division f = a / b puts "division #{f}" # Modulo g = a % b puts "modulo #{g}" # Exponent h = a ** b puts "exponent #{h}" # Unary minus i= -a puts "unary minus #{i}" Output: addition 4.1 subtraction 0.1 multiplication 4.2 division 1.05 modulo 0.1 exponent 4.41 unary minus -2.1 <=> : This method returns -1, 0, or +1 depends upon float. If float is less then numeric value then it will return -1, if float is equal to numeric value, then it will returns 0, or if float is greater then numeric value, then it will return +1. float <=> numeric --> 1, 0, +1 Example: Ruby # Ruby program to illustrate # <=> Method puts 2.1 <=> 4 puts 2.0 <=> 2 puts 4.6 <=> 2 Output: -1 0 1 == : This method returns true if the obj is equal to float otherwise it returns false. float == obj --> true or false Example: Ruby # Ruby program to illustrate # == Method puts 3.8 == 4 puts 3.8 == 3.8 Output: false true abs : This method return absolute value of float. float.abs --> numeric Example: Ruby # Ruby program to illustrate # abs Method puts (-54.56).abs puts (-65.04).abs Output: 54.56 65.04 ceil : This method returns the smallest Integer greater than or equal to float. The return type of this method is int. float.ceil --> int Example: Ruby # Ruby program to illustrate # ceil Method puts (4.1).ceil puts (4.0).ceil puts (-4.1).ceil Output: 5 4 -4 divmod : This method will return an array that contains the quotient and modulus obtained by dividing num by numeric. float.divmod(numeric) --> array Example: Ruby # Ruby program to illustrate # divmod Method p (45.0.divmod 5) p (98.0.divmod 5) Output: [9, 0.0] [19, 3.0] eql? : This method check if the obj is Float and contains the same value as in float. If they contains same value then it will return true, otherwise return false. The return type of this method is boolean. float.eql?(obj) --> true or false Example: Ruby # Ruby program to illustrate # eql? Method puts 4.2.eql?(2) puts 1.2.eql?(1.2) Output: false true finite? : This method check if the float is a valid IEEE floating-point number. If float is valid IEEE floating-point number then it will return true otherwise it will return false. float.finite? --> true or false Example: Ruby # Ruby program to illustrate # finite? Method puts (45.0).finite? puts (45.0/0.0).finite? Output: true false floor : This method returns largest integer less than or equal to float. float.floor --> int Example: Ruby # Ruby program to illustrate # floor Method puts 2.2. floor puts (-4.6).floor Output: 2 -5 infinite? : This method returns nil, -1, or +1 it depends upon float. If float is finite, then it return nil, if float is -infinite, then it return -1, or if float is +infinite then it return +1. float.infinite? --> nil, -1, +1 Example: Ruby # Ruby program to illustrate # infinite? Method puts (1.1).infinite? puts (-1.1/0.0).infinite? puts (+1.1/0.0).infinite? Output: nil -1 1 modulo: This method is similar to Float#% method. float.modulo(numeric) --> numeric Example: Ruby # Ruby program to illustrate # modulo Method puts 32.45.modulo(20) Output: 12.450000000000003 nan? : This method return true if float is an invalid IEEE floating-point number otherwise it return false. The return type of this method is boolean. float.nan? --> true or false Example: Ruby # Ruby program to illustrate # nan? Method puts (-2.2). nan? puts (0.0/0.0). nan? Output: false true round: This method rounds off float to the nearest integer value. The return type of this method is int. float..round(digits=0) --> numeric Example: Ruby # Ruby program to illustrate # round Method puts 6.7.round puts (-8.9).round Output: 7 -9 to_f : This method return float. float.to_f --> float to_i : This method return float truncate to the integer. The return type of this method is int. float.to_i --> int Example: Ruby # Ruby program to illustrate # to_i Method puts 5.6.to_i Output: 5 to_int : This method is similar to Float#to_i. float.to_int --> int to_s: This method returns a string that contains a representation of self, as well as a fixed or exponential form of numbering. The call may return NaN, infinity and -infinity. float.to_s --> string truncate : This method is equal to Float#to_i method. The return type of this method is int.float.truncate zero? : This method return true if float is 0.0 otherwise return false. The return type of this method is boolean. float.zero? --> true or false Example: Ruby # Ruby program to illustrate # zero? Method puts (0.0).zero? puts (1.4).zero? Output: true false Float class contains Constants which are listed as follows: Constants Description DIG It holds minimum number of significant decimal digits in a double-precision floating point and it defaults to 15. EPSILON It holds difference between 1 and the smallest double-precision floating point number greater than 1 and defaults to 2.2204460492503131e-16. MANT_DIG It holds the number of mantissa digits of base RADIX. Defaults to 53. MAX It holds largest possible integer in a double-precision floating point number and it defaults to 1.7976931348623157e+308. MAX_10_EXP It represent the largest positive exponent in a double-precision floating point where 10 raised to this power minus 1. Defaults to 308. MAX_EXP It is the largest possible exponent value in a double-precision floating point which defaults to 1024. MIN It is the smallest positive normalized number in a double-precision floating point. Defaults to 2.2250738585072014e-308. MIN_10_EXP It is the smallest negative exponent in a double-precision floating point where 10 raised to this power minus 1. Defaults to -307. MIN_EXP It is the smallest possible exponent value in a double-precision floating point. Defaults to -1021 RADIX The radix of floating-point representations or in other words, it is a base of floating-point numbers. Defaults to 2 on most systems, which would represent a base-10 decimal ROUND It represents the rounding mode for floating-point operations. The values includes are: -1: if the mode is indeterminate 0: if rounding towards zero 1: if the rounding is nearest to representable value 2: if rounding is towards +infinite 3: if rounding is towards +infinite NaN It is an expression representing a value which is "not a number". INFINITY It is an expression representing positive infinity. Reference: https://ruby-doc.org/core-2.4.0/Float.html Create Quiz Comment A ankita_saini Follow 0 Improve A ankita_saini Follow 0 Improve Article Tags : Misc Ruby Ruby-Built-in-class Explore OverviewRuby For Beginners3 min readRuby Programming Language (Introduction)4 min readComparison of Java with Other Programming Languages4 min readSimilarities and Differences between Ruby and C language3 min readSimilarities and Differences between Ruby and C++3 min readEnvironment Setup in Ruby3 min readHow to install Ruby on Linux?2 min readHow to install Ruby on Windows?2 min readInteresting facts about Ruby Programming Language2 min readBasicsRuby | Keywords4 min readRuby | Data Types3 min readRuby Basic Syntax3 min readHello World in Ruby2 min readRuby | Types of Variables4 min readGlobal Variable in Ruby2 min readComments in Ruby2 min readRuby | Ranges4 min readRuby Literals4 min readRuby Directories5 min readRuby | Operators11 min readOperator Precedence in Ruby2 min readOperator Overloading in Ruby5 min readRuby | Pre-define Variables & Constants5 min readRuby | unless Statement and unless Modifier2 min readControl StatementsRuby | Decision Making (if, if-else, if-else-if, ternary) | Set - 13 min readRuby | Loops (for, while, do..while, until)5 min readRuby | Case Statement3 min readRuby | Control Flow Alteration7 min readRuby Break and Next Statement2 min readRuby redo and retry Statement2 min readBEGIN and END Blocks In Ruby2 min readFile Handling in Ruby4 min readMethodsRuby | Methods3 min readMethod Visibility in Ruby3 min readRecursion in Ruby4 min readRuby Hook Methods5 min readRuby | Range Class Methods5 min readThe Initialize Method in Ruby2 min readRuby | Method overriding2 min readRuby Date and Time3 min readOOP ConceptsObject-Oriented Programming in Ruby | Set 19 min readObject Oriented Programming in Ruby | Set-28 min readRuby | Class & Object4 min readPrivate Classes in Ruby3 min readFreezing Objects | Ruby2 min readRuby | Inheritance4 min readPolymorphism in Ruby3 min readRuby | Constructors2 min readRuby | Access Control8 min readRuby | Encapsulation2 min readRuby Mixins3 min readInstance Variables in Ruby3 min readData Abstraction in Ruby3 min readRuby Static Members3 min readExceptionsRuby | Exceptions4 min readRuby | Exception handling6 min readCatch and Throw Exception In Ruby3 min readRaising Exceptions in Ruby4 min readRuby | Exception Handling in Threads | Set - 12 min readRuby | Exception Class and its Methods3 min readRuby RegexRuby | Regular Expressions3 min readRuby Search and Replace2 min readRuby ClassesRuby | Float Class7 min readRuby | Integer Class3 min readRuby | Symbol Class5 min readRuby | Struct Class5 min readRuby | Dir Class and its methods3 min readRuby | MatchData Class4 min readRuby ModuleRuby | Module4 min readRuby | Comparable Module3 min readRuby | Math Module4 min readInclude v/s Extend in Ruby2 min readCollectionsRuby | Arrays4 min readRuby | String Basics5 min readRuby | String Interpolation3 min readRuby | Hashes Basics4 min readRuby | Hash Class12 min readRuby | Blocks6 min read Like