Float Type क्या है
एक Float एक decimal point वाला number है, एक measured length की तरह। यह ऐसी चीज़ों के लिए है जो whole नहीं हैं, जैसे 3.14।
In this page:
Syntax
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
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
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
==से floats compare करना, जैसे0.1 + 0.2 == 0.3, जोfalseहै; एक tolerance से compare करें याroundइस्तेमाल करें।10 / 3लिखना और3.33की उम्मीद करना, जब दोनों operands integers हों और result3हो।- money के लिए
Floatइस्तेमाल करना, जब rounding errors जमा हो सकती हैं औरBigDecimalयाIntegerमें cents safer हैं।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: