Type Conversion क्या है
In this page:
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
// 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)")
Login to try C/C++/Java/PHP code in the editor
Converting Strings to Numbers
एक String को एक number में convert करना एक failable initializer उपयोग करता है जो एक optional return करता है, क्योंकि हर string एक valid number नहीं है।
उदाहरण: Converting Strings to Numbers
// 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))")
Login to try C/C++/Java/PHP code in the editor
Converting Numbers to Strings
एक number को एक String में बदलना या तो String(_:) initializer से या string interpolation के ज़रिए किया जाता है, दोनों वही text representation produce करते हुए।
उदाहरण: Converting Numbers to Strings
// 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)")
Login to try C/C++/Java/PHP code in the editor
Truncation When Converting Double to Int
एक Double को एक Int में convert करना fractional part को round करने के बजाय truncate करता है, इसलिए 4.9 4 बन जाता है, 5 नहीं।
.rounded() उपयोग करें।उदाहरण: Truncation When Converting Double to Int
// 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)")
Login to try C/C++/Java/PHP code in the editor
- एक non-numeric string को
Intमें convert करने की कोशिश करना, जैसेInt("hello"), failable initializer से resultingnilhandle किए बिना। - यह मान लेना कि numeric conversions जैसे
IntसेDoubleअपने आप होते हैं; Swift को हमेशा एक explicit conversion call चाहिए। - यह भूल जाना कि एक
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: