Type Conversion क्या है
In this page:
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
// 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")
}
Login to try C/C++/Java/PHP code in the editor
Converting Between Numeric Types
toInt(), toLong(), toFloat(), और toDouble() जैसे functions किसी number को एक numeric type से दूसरे में convert करते हैं, उचित रूप से truncate या widen करते हुए।
उदाहरण: Converting Between Numeric Types
// 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")
}
Login to try C/C++/Java/PHP code in the editor
Converting Strings to Numbers
toInt(), toDouble(), और similar functions किसी String को एक number में parse करते हैं, लेकिन अगर text एक valid number न हो तो वे एक NumberFormatException फेंकते हैं।
उदाहरण: Converting Strings to Numbers
// 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}")
}
Login to try C/C++/Java/PHP code in the editor
Safe Conversion with OrNull
toIntOrNull(), toDoubleOrNull(), और similar functions string के parse न हो पाने पर throw करने के बजाय null return करते हैं, जो untrusted input handle करने के लिए ज़्यादा सुरक्षित है।
उदाहरण: Safe Conversion with OrNull
// 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()}")
}
Login to try C/C++/Java/PHP code in the editor
- Java/C जैसी implicit widening conversions की उम्मीद करना, जैसे किसी
Intको सीधेDoublevariable को assign करना; Kotlin को एक explicittoDouble()call चाहिए। - proper
toInt()/toDouble()functions के बजाय numeric conversions के लिएasका उपयोग करना, जो type casting के लिए है, conversion के लिए नहीं। - किसी 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()और similarOrNullvariants conversion fail होने पर throw करने के बजायnullreturn करते हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: