1. Types Double and Float and their literals
When you’re just starting to program, it can feel like Int is enough for everything: add up a sum, subtract, multiply, compare. But as soon as a task includes a fractional part (percentages, averages, measurements, speed, temperature, “half” of something), Int starts behaving like a very strict accountant: “fractions don’t exist, here’s an integer — and don’t argue”.
Floating-point numbers are needed where the fractional part is not decoration but the whole point. For example, “average speed 37.5 km/h”, “discount 12.5%”, “temperature 36.6”, “density 1.225” — all of this is poorly expressed as integers. In theory, you can store “36.6” as “366” and remember that those are tenths, but that’s an agreement that’s easy to forget. We’ll even use this as a learning technique (for example, storing money in cents), but only consciously.
In Kotlin (as in many languages), there are separate types for fractional computations. Today we’re interested in two: Double and Float. They are similar in meaning (“numbers with a fractional part”), but they differ in precision and in how often you’ll choose them in everyday tasks.
Double — the default choice for fractions
Double is the main workhorse for floating-point numbers in Kotlin. It’s often the “default” choice when you need a fractional part and don’t have specific constraints. In practice, you’ll see Double all the time: in formulas, calculations, geometry, percentages, and almost anywhere “non-integer” results appear.
Double has a very convenient clue: if you write a number with a decimal point, Kotlin usually treats it as a Double. For example, in the Kotlin documentation you can see the example val PI = 3.14 — the type of such a value is inferred automatically.
Let’s lock in the main idea: a literal with a decimal point (10.0, 2.5, 3.14) is a Double.
Mini example — let’s just look at the computation and output:
fun main() {
val pi = 3.14
val radius = 2.0
val area = pi * radius * radius
println("pi = $pi") // pi = 3.14
println("area = $area") // area = 12.56
}
Notice: I intentionally wrote radius = 2.0, not 2. This is not “for looks” — it’s to start working in the world of floating-point numbers right away and avoid surprises with division (we’ll get to that in the next section).
Float — also a floating type, but the literal requires the f suffix
Float is also a floating-point number, just usually less precise and more compact. For a beginner, it’s less important to know “how many bits” and more important to understand two practical facts: first, Float shows up less often in beginner exercises, and second, Kotlin won’t let you accidentally write a Float literal without a special marker.
The main idea: if you want a literal of type Float, you must add the f suffix. For example, 2.5f. This isn’t Kotlin being picky — it’s a way to force you to be explicit: “yes, I really want Float, I thought about it, I agree”.
This style also appears in Kotlin documentation examples: for instance, -3.4f — that is exactly a Float.
Mini example:
fun main() {
val a: Float = 2.5f
val b: Double = 2.5
println("a = $a") // a = 2.5
println("b = $b") // b = 2.5
}
And one more important moment you will almost certainly run into in the first days: if you write val x: Float = 2.5 without f, the compiler will be unhappy. It sees 2.5 as a Double, and Kotlin doesn’t like “silent” numeric conversions. That’s good: less magic — fewer unexpected bugs.
For now, remember a simple rule: if you’re not sure you need Float, use Double. Float is more often used in tasks like graphics or large arrays of numbers where saving memory matters, but in our first programs it’s usually not critical.
Literals: 10 and 10.0 are different types
Now comes the moment where your brain likes to go “oh, got it” — and then immediately make a mistake on the next line. In Kotlin, two visually similar numbers can be different types simply because one has a decimal point and the other doesn’t. 10 is an integer (usually Int), and 10.0 is already fractional (Double).
This matters because the type affects how numeric operations work. Especially division. Especially when you expect a fractional result but get an integer and think: “Who stole my 0.5?”
Mini example:
fun main() {
val a = 10 // Int (integer)
val b = 10.0 // Double (floating-point)
println("a = $a") // a = 10
println("b = $b") // b = 10.0
}
Yes, the output already hints at it: 10.0 looks like a number with a fractional part (even if the fraction is zero). That’s useful: visually, you can tell you’re working in a different “mode”.
From here comes a simple (and very practical) trick: if you want to “move computations into the world of fractions”, it’s enough to put .0 somewhere in the expression. It sounds too simple, but that’s exactly what people often do in basic tasks until they move on to other type conversions.
3. Division: integers vs floating-point
Division is the main source of surprise for beginners. And the surprise is honest: “I divided like this in school — why is it different here?” The answer is simple: the computer performs division with types in mind. If you divide an integer by an integer, the result is also an integer. The fractional part is simply discarded.
And this behavior is not a “Kotlin bug” — it’s standard integer arithmetic in programming: Int / Int -> Int. To get a fractional result, at least one operand must be Double or Float.
Mini example worth remembering like the password to the door “into the world of fractions”:
fun main() {
val a = 5 / 2
val b = 5.0 / 2
println("a = $a") // a = 2
println("b = $b") // b = 2.5
}
Now let’s try to write a small piece of business logic. Let’s calculate a receipt total, but to avoid entering floating-point numbers (that’s a separate topic), we’ll store money in cents as Int, and for display convert it to euros by dividing by 100.0.
Note: we divide by 100.0, not by 100. This is crucial.
fun main() {
val totalCents = 2599 // 25.99 (some euros/dollars — doesn’t matter)
val total = totalCents / 100.0 // Double: 25.99
println("totalCents = $totalCents") // totalCents = 2599
println("total = $total") // total = 25.99
}
If we wrote totalCents / 100, we’d get 25, and you would lose 99 cents. The program would happily “save” the user money, but unfortunately only in the console.
To reinforce this, let’s add one more typical operation: split the bill among several people. Let the number of people be entered as an integer — that’s familiar.
fun main() {
val totalCents = 2599
val people = 3
val total = totalCents / 100.0
val perPerson = total / people
println("total = $total") // total = 25.99
println("perPerson = $perPerson") // perPerson = 8.663333333333334
}
Yes, the output is “long”. That’s normal: we’re not rounding or formatting nicely yet. Our goal right now is to understand why we got a non-integer value at all and how to get a fractional result. Pretty output comes later; today we’re “learning to walk”, not “learning to dance”.
A small reminder diagram for how to think about division:
flowchart TD
A["Vy pishite delenie x / y"] --> B{"x i y — tselye Int?"}
B -- "da" --> C["Rezul'tat tozhe Int drobnaya chast' teryaetsya"]
B -- "net (est' Double/Float)" --> D["Rezul'tat veshchestvennyy drobnaya chast' sokhranyaetsya"]
If you’re unsure, just ask yourself: “Does this expression contain at least one number with a decimal point?” If not, there’s a good chance you’ll get integer behavior.
4. Scientific notation: 1e3 and 1e-3
When numbers get very large or very small, writing them “straight” becomes inconvenient. Nobody wants to read 0.000001 and count zeros with their eyes — especially when the eyes are already tired after the tenth println. For such cases, there is scientific notation (also called exponential notation): 1e3 means “1 multiplied by 10 to the power of 3”.
In Kotlin, such literals are also floating-point numbers (usually Double). This is convenient: you can write coefficients, tolerances, scales, and other “small/large” values in a short and readable way.
Mini example:
fun main() {
val big = 1e3 // 1000.0
val small = 1e-3 // 0.001
println("big = $big") // big = 1000.0
println("small = $small") // small = 0.001
}
Why is this useful specifically for you as beginners? Because formulas often contain scaling coefficients. For example, “convert millimeters to meters” — multiply by 1e-3. “convert kilograms to grams” — multiply by 1e3. And this notation is much less likely to trigger the “put the zero in the wrong place” mistake.
At the same time, don’t be scared if Kotlin sometimes prints the number back as 1.0E-3 or something similar. That’s just a display format. It’s the same number, just printed more compactly.
5. What to choose: Double or Float
You want a clear rule so you don’t suffer? Good news: at the level of our tasks, it really is simple. If you’re writing a typical console program, calculating percentages or averages, converting units, computing the area of a circle, or splitting a bill with friends, choose Double. It’s the most familiar and “wide” option.
Float is worth choosing when you have a specific reason. Most often the reason sounds like “we have a lot of numbers and we’re saving memory” or “we work in an environment where using Float is common (for example, some graphics calculations)”. But those stories usually start later, when you already write code confidently and understand where exactly you need it.
If you want to lock this in your head a bit more reliably, you can use a simple table (not as an exam cheat sheet, but as a reminder of “what am I doing at all”):
| Type | How the literal looks | Example | When you need it at the beginning |
|---|---|---|---|
|
without a decimal point | |
counters, quantities, indexes |
|
with a decimal point / e | |
fractional calculations “by default” |
|
with a decimal point + f | |
rarely; only if there’s a reason |
And the last important rule that you can treat as “ironclad” today: if you need a fractional result, make sure the expression is evaluated as Double (or Float). The simplest way is to use at least one literal with a decimal point: 100.0, 5.0, 2.0. It sounds almost like a lifehack, but early on it saves you from a huge number of mistakes.
6. Common mistakes
Mistake No. 1: expecting 5 / 2 to produce 2.5.
This is a classic. The problem is that both numbers are integers, so the result is also an integer. The fix is very simple: make at least one number floating-point, for example 5.0 / 2 or 5 / 2.0. A useful habit is to immediately write “base” numbers as Double (with a decimal point) in problems with averages and percentages so you don’t forget.
Mistake No. 2: trying to assign a Float without the f suffix.
val x: Float = 2.5 looks human-logical, but Kotlin sees 2.5 as a Double and doesn’t want to “silently” turn one into the other. The correct form: 2.5f. If you’re not sure you specifically need Float, it’s better not to touch it at all and just use Double — fewer reasons to argue with the compiler.
Mistake No. 3: dividing money/percentages using integer division and “losing the tail”.
When you store money in cents (Int) and convert it to Swiss francs, it’s important to divide by 100.0, not by 100. Dividing by 100 will produce an integer and discard the remainder. This mistake is especially sneaky because the program works “fine” — the result just becomes wrong, and the wrongness isn’t always obvious right away.
Mistake No. 4: being afraid of scientific notation and treating it as a “different type”.
1e-3 is not some special secret type — it’s a normal floating-point literal. It means “multiply by 10 to the power of -3”. If you see an output format like 1.0E-3, that’s also normal: Kotlin (more precisely, the standard number formatting) sometimes prints small values in exponential form to avoid drawing a lot of zeros.
Mistake No. 5: choosing Float “just because it’s shorter”.
Sometimes it feels like Float is a “lighter Double”, so it must be better. In practice, in beginner tasks it more often creates extra hassle: literals require f, type errors happen more often, and you don’t feel any benefit. So for now, stick to the calm rule: Double by default; Float only when you consciously need it.
GO TO FULL VERSION