← Back to Ruby Course | Chapter 2: Variables & Types | Lesson 3 of 7

Float Type क्या है

एक Float एक decimal point वाला number है, एक measured length की तरह। यह ऐसी चीज़ों के लिए है जो whole नहीं हैं, जैसे 3.14।

In this page:

  1. Float Type
Syntax
ruby
number = 3.14
number.round(2)
number.class    # Float

Float Type

Float Ruby में floating-point (decimal) numbers represent करता है, standard IEEE 754 double-precision behavior follow करते हुए, usual floating-point rounding quirks समेत।

/ से दो integers divide करना integer division देता है, इसलिए fractional result पाने के लिए कम से कम एक operand Float होना चाहिए। to_f एक value को explicitly एक float में convert करता है।

Note: Integer division की बजाय एक fractional result force करने के लिए 10.0 / 3 से divide करें (या एक operand पर .to_f call करें)।

उदाहरण: Float Type

markup
price = 19.99
puts price.class
puts price * 2

puts 10 / 3          # integer division
puts 10.0 / 3         # float division
puts 10.to_f / 3
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. == से floats compare करना, जैसे 0.1 + 0.2 == 0.3, जो false है; एक tolerance से compare करें या round इस्तेमाल करें।
  2. 10 / 3 लिखना और 3.33 की उम्मीद करना, जब दोनों operands integers हों और result 3 हो।
  3. money के लिए Float इस्तेमाल करना, जब rounding errors जमा हो सकती हैं और BigDecimal या Integer में cents safer हैं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.