Skip to content

Commit 9767912

Browse files
committed
update: various small improvements to the Numbers page
1 parent 8914262 commit 9767912

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

docs/topics/numbers.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ fun main() {
186186
//sampleEnd
187187
}
188188
```
189+
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}
189190

190191
All number types support conversions to other types:
191192

@@ -200,8 +201,14 @@ In many cases, there is no need for explicit conversions because the type is inf
200201
and arithmetical operators are overloaded for appropriate conversions, for example:
201202

202203
```kotlin
203-
val l = 1L + 3 // Long + Int => Long
204+
fun main() {
205+
//sampleStart
206+
val l = 1L + 3 // Long + Int => Long
207+
println(l is Long) // true
208+
//sampleEnd
209+
}
204210
```
211+
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}
205212

206213
## Operations on numbers
207214

@@ -232,7 +239,7 @@ fun main() {
232239
//sampleStart
233240
val x = 5 / 2
234241
println(x == 2.5) // ERROR: Operator '==' cannot be applied to 'Int' and 'Double'
235-
println(x == 2)
242+
println(x == 2) // true
236243
//sampleEnd
237244
}
238245
```
@@ -244,13 +251,13 @@ This is true for a division between any two integer types:
244251
fun main() {
245252
//sampleStart
246253
val x = 5L / 2
247-
println(x == 2L)
254+
println(x == 2L) // But not `x == 2`, as Long (x) cannot be compared to Int (2)
248255
//sampleEnd
249256
}
250257
```
251258
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}
252259

253-
To return a floating-point type, explicitly convert one of the arguments to a floating-point type:
260+
To return a division result with the fractional part, explicitly convert one of the arguments to a floating-point type:
254261

255262
```kotlin
256263
fun main() {
@@ -273,12 +280,10 @@ fun main() {
273280
//sampleStart
274281
val x = 1
275282
val xShiftedLeft = (x shl 2)
276-
// Prints "4"
277-
println(xShiftedLeft)
283+
println(xShiftedLeft) // Prints "4"
278284

279285
val xAnd = x and 0x000FF000
280-
// Prints "0"
281-
println(xAnd)
286+
println(xAnd) // Prints "0"
282287
//sampleEnd
283288
}
284289
```

0 commit comments

Comments
 (0)