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

Type Conversion क्या है

Type conversion का मतलब है एक तरह की value, जैसे एक number, को एक अलग तरह में, जैसे text, बदलना ताकि आपके program के अलग-अलग हिस्से एक साथ काम कर सकें।
Syntax
swift
let asDouble = Double(intValue)
let asInt = Int(stringValue)       // returns Int? (optional)
let asString = String(number)

Numeric Type Conversion

Int और Double जैसे numeric types के बीच convert करने के लिए, आप value को target type के initializer में wrap करते हैं -- Swift कभी numbers को implicitly convert नहीं करता।

उदाहरण: Numeric Type Conversion

markup
// Declare a constant `wholeNumber`, set to `7`
let wholeNumber = 7
// Declare a constant `asDouble`, set to `Double(wholeNumber)`
let asDouble = Double(wholeNumber)
// Print "Int: \(wholeNumber), as Double: \(asDouble)" to the console
print("Int: \(wholeNumber), as Double: \(asDouble)")

Converting Strings to Numbers

एक String को एक number में convert करना एक failable initializer उपयोग करता है जो एक optional return करता है, क्योंकि हर string एक valid number नहीं है।

उदाहरण: Converting Strings to Numbers

markup
// Declare a constant `validText`, set to `"42"`
let validText = "42"
// Declare a constant `invalidText`, set to `"hello"`
let invalidText = "hello"
// Declare a constant `validNumber`, set to `Int(validText)`
let validNumber = Int(validText)
// Declare a constant `invalidNumber`, set to `Int(invalidText)`
let invalidNumber = Int(invalidText)
// Print "Valid: \(String(describing: validNumber))" to the console
print("Valid: \(String(describing: validNumber))")
// Print "Invalid: \(String(describing: invalidNumber))" to the console
print("Invalid: \(String(describing: invalidNumber))")

Converting Numbers to Strings

एक number को एक String में बदलना या तो String(_:) initializer से या string interpolation के ज़रिए किया जाता है, दोनों वही text representation produce करते हुए।

उदाहरण: Converting Numbers to Strings

markup
// Declare a constant `count`, set to `100`
let count = 100
// Declare a constant `asText`, set to `String(count)`
let asText = String(count)
// Print `"Converted: " + asText` to the console
print("Converted: " + asText)
// Print "Interpolated: \(count)" to the console
print("Interpolated: \(count)")

Truncation When Converting Double to Int

एक Double को एक Int में convert करना fractional part को round करने के बजाय truncate करता है, इसलिए 4.9 4 बन जाता है, 5 नहीं।

Note: अगर आप truncation के बजाय rounding चाहते हैं तो convert करने से पहले .rounded() उपयोग करें।

उदाहरण: Truncation When Converting Double to Int

markup
// Declare a constant `price`, set to `4.9`
let price = 4.9
// Declare a constant `truncated`, set to `Int(price)`
let truncated = Int(price)
// Print "4.9 truncated to Int: \(truncated)" to the console
print("4.9 truncated to Int: \(truncated)")
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. एक non-numeric string को Int में convert करने की कोशिश करना, जैसे Int("hello"), failable initializer से resulting nil handle किए बिना।
  2. यह मान लेना कि numeric conversions जैसे Int से Double अपने आप होते हैं; Swift को हमेशा एक explicit conversion call चाहिए।
  3. यह भूल जाना कि एक Double को Int में convert करना decimal part को round करने के बजाय truncate करता है।
चैप्टर सारांश
  • Numeric types के बीच convert करने के लिए एक explicit initializer call चाहिए, जैसे Double(someInt)।
  • एक String को एक number में convert करना एक failable initializer उपयोग करता है, जैसे Int("42"), जो एक optional return करता है।
  • एक number को एक String में convert करना String(someNumber) या interpolation से किया जाता है।
  • Int(someDouble) round करने के बजाय zero की ओर truncate करता है।
🔒

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.