← Back to Swift Course | Chapter 2: Variables & Types | Lesson 5 of 8

Doubles और Floats

Doubles और floats वे numbers हैं जिनमें एक decimal point हो सकता है, जैसे 3.14 या 0.5।
Syntax
swift
let name = 3.14               // Double
let name: Float = 3.14        // less precise
let result = Double(intValue) / doubleValue

Declaring Doubles

Double Swift का default floating-point type है, अपने आप उपयोग होता है जब एक literal में एक decimal point हो।

उदाहरण: Declaring Doubles

markup
// Declare a constant `price`, set to `19.99`
let price = 19.99
// Declare a constant `taxRate`, set to `0.08`
let taxRate = 0.08
// Print "Price: \(price), tax rate: \(taxRate)" to the console
print("Price: \(price), tax rate: \(taxRate)")

Float vs Double

Float कम memory उपयोग करता है (32 bits) लेकिन Double (64 bits) से कम precision रखता है। Swift default रूप से Double infer करता है, इसलिए Float को एक explicit annotation से मांगना चाहिए।

उदाहरण: Float vs Double

markup
// Declare a constant `preciseValue` (a Double), set to `3.14159265358979`
let preciseValue: Double = 3.14159265358979
// Declare a constant `lessPrecise` (a Float), set to `3.14159265358979`
let lessPrecise: Float = 3.14159265358979
// Print "Double: \(preciseValue)" to the console
print("Double: \(preciseValue)")
// Print "Float: \(lessPrecise)" to the console
print("Float: \(lessPrecise)")

Converting Between Int and Double

Swift numeric types के बीच कभी चुपचाप convert नहीं करता; आपको एक value को explicitly wrap करना चाहिए, जैसे Double(someInt), calculations के लिए एक integer को एक floating-point number में convert करने के लिए।

Note: Double(items) के बिना, एक Double को एक Int से divide करना compile भी नहीं होता -- Swift matching types चाहता है।

उदाहरण: Converting Between Int and Double

markup
// Declare a constant `items`, set to `3`
let items = 3
// Declare a constant `totalCost`, set to `10.0`
let totalCost = 10.0
// Declare a constant `average`, set to `totalCost / Double(items)`
let average = totalCost / Double(items)
// Print "Average cost per item: \(average)" to the console
print("Average cost per item: \(average)")
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. Arithmetic के बाद दो Double values को == से compare करना और exact equality की उम्मीद करना, जब floating-point rounding उन्हें थोड़ा अलग बना सकता है।
  2. Double का इरादा होने पर Float उपयोग करना; Double Swift का default है और ज़्यादा precision रखता है, इसलिए इन्हें mix करने के लिए explicit conversion चाहिए।
  3. दो Int values को divide करना एक decimal result की उम्मीद से, यह भूल जाना कि Int / Int एक Double produce करने के बजाय truncate करता है।
चैप्टर सारांश
  • Double एक 64-bit floating-point type है और decimal numbers के लिए Swift का default choice है।
  • Float एक 32-bit floating-point type है, कम precise लेकिन कभी-कभी memory save करने के लिए उपयोग होता है।
  • Floating-point arithmetic में छोटी rounding errors हो सकती हैं, इसलिए exact == comparisons से बचें।
  • एक Int को एक Double में convert करने के लिए एक explicit Double(someInt) call चाहिए।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 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.