Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

float
ruby
communitycreator

How to return the next float value after the previous one in Ruby

okwudili onyejiaku

Overview

We use the next_float method to return the next floating-point number after a specific floating-point number.

Syntax

float_value.next_float

Parameters

float_value: This is the float value whose next value we want to find.

Return value

The next float value after the float_value is returned.

Code example

# create some float values
f1 = 2.3
f2 = -1.0
f3 = 0.1111111 
f4 = 200.00

# get next values
# and print results
puts f1.next_float
puts f2.next_float
puts f3.next_float
puts f4.next_float
Get the next floating-point number after a specified floating-point number

Code explanation

  • Lines 2–5: We create some float values —f1, f2, f3, f4, and f5.
  • Lines 9–12: We invoke the next_float method on the float values we created, and print the results.

RELATED TAGS

float
ruby
communitycreator
RELATED COURSES

View all Courses

Keep Exploring