← Back to Kotlin Course | Chapter 2: Variables & Types | Lesson 5 of 7

Type Conversion क्या है

Type conversion एक तरह की value, जैसे number, को दूसरी तरह की, जैसे text, में बदलना है, ताकि data के अलग-अलग टुकड़े साथ काम कर सकें।
Syntax
kotlin
val asDouble = intValue.toDouble()      // toInt(), toLong(), toFloat()
val number = stringValue.toInt()        // throws if not a valid number
val safe = stringValue.toIntOrNull()    // null if invalid

No Implicit Widening

कुछ languages के विपरीत, Kotlin कभी किसी Int को अपने आप Double में convert नहीं करता भले ही एक Double हर Int value को represent कर सके। numeric types के बीच assign करने के लिए हमेशा एक explicit conversion function चाहिए।

उदाहरण: No Implicit Widening

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `wholeNumber` (an Int), set to `10`
    val wholeNumber: Int = 10
    // Declare a read-only variable `decimalNumber` (a Double), set to `wholeNumber.toDouble()`
    val decimalNumber: Double = wholeNumber.toDouble()
    // Print "Int: $wholeNumber, Double: $decimalNumber" to the console, with a trailing newline
    println("Int: $wholeNumber, Double: $decimalNumber")
}

Converting Between Numeric Types

toInt(), toLong(), toFloat(), और toDouble() जैसे functions किसी number को एक numeric type से दूसरे में convert करते हैं, उचित रूप से truncate या widen करते हुए।

Note: किसी Double को Int में convert करना round करने के बजाय decimal हिस्से को truncate कर देता है।

उदाहरण: Converting Between Numeric Types

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `price` (a Double), set to `19.99`
    val price: Double = 19.99
    // Declare a read-only variable `roundedDown` (an Int), set to `price.toInt()`
    val roundedDown: Int = price.toInt()
    // Print "Price: $price, as Int: $roundedDown" to the console, with a trailing newline
    println("Price: $price, as Int: $roundedDown")
}

Converting Strings to Numbers

toInt(), toDouble(), और similar functions किसी String को एक number में parse करते हैं, लेकिन अगर text एक valid number न हो तो वे एक NumberFormatException फेंकते हैं।

उदाहरण: Converting Strings to Numbers

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `text`, set to `"42"`
    val text = "42"
    // Declare a read-only variable `number`, set to `text.toInt()`
    val number = text.toInt()
    // Print "Parsed number: ${number + 8}" to the console, with a trailing newline
    println("Parsed number: ${number + 8}")
}

Safe Conversion with OrNull

toIntOrNull(), toDoubleOrNull(), और similar functions string के parse न हो पाने पर throw करने के बजाय null return करते हैं, जो untrusted input handle करने के लिए ज़्यादा सुरक्षित है।

उदाहरण: Safe Conversion with OrNull

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `goodInput`, set to `"123"`
    val goodInput = "123"
    // Declare a read-only variable `badInput`, set to `"abc"`
    val badInput = "abc"
    // Print "Good: ${goodInput.toIntOrNull()}" to the console, with a trailing newline
    println("Good: ${goodInput.toIntOrNull()}")
    // Print "Bad: ${badInput.toIntOrNull()}" to the console, with a trailing newline
    println("Bad: ${badInput.toIntOrNull()}")
}
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. Java/C जैसी implicit widening conversions की उम्मीद करना, जैसे किसी Int को सीधे Double variable को assign करना; Kotlin को एक explicit toDouble() call चाहिए।
  2. proper toInt()/toDouble() functions के बजाय numeric conversions के लिए as का उपयोग करना, जो type casting के लिए है, conversion के लिए नहीं।
  3. किसी non-numeric string पर toInt() call करना और परिणामी NumberFormatException को handle न करना।
चैप्टर सारांश
  • Kotlin कभी implicit numeric conversions नहीं करता; Int और Double जैसे types के बीच हर conversion explicit होना चाहिए।
  • toInt(), toDouble(), toLong(), और toString() जैसे conversion functions numeric types और strings के बीच convert करते हैं।
  • किसी non-numeric string को toInt() से convert करना runtime पर एक NumberFormatException फेंकता है।
  • toIntOrNull() और similar OrNull variants conversion fail होने पर throw करने के बजाय null return करते हैं।
🔒

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.