TryBugHunt
goType errorMedium

Adding up 0.29 gives 28 cents

BugFind the bug and fix the code

What it should do

totalCents(prices) converts a list of prices in dollars to whole cents and adds them up. totalCents([0.57, 1.1]) returns 167.

What happens instead

totalCents([0.29]) returns 28, and totalCents([0.57, 1.1]) returns 166.

package main

func totalCents(prices []float64) int {
    total := 0
    for _, p := range prices {
        total += int(p * 100)
    }
    return total
}

Test a custom input to verify an edge case (e.g. empty lists, negative numbers, duplicates).

⏱️0:00
Hints
🏆 0 hints = Flawless Solve
Think & Test Cooldown
2:00 remaining

Take a moment to read the bug symptom and trace the code with Run & Check before unlocking Hint 1.

Hint 2
Unlocks after Hint 1
Hint 3
Unlocks after Hint 2

This bug belongs to the type errors pattern

Integer division that drops the fraction, overflow, and conversions that change the value.

What you'll practise

A medium Go debugging exercise about type errors: integer division that drops the fraction, overflow, and conversions that change the value.

The checks your fix must pass

Signs of this kind of bug

Spoiler: why this bug happens(try it first; the hints get warmer one at a time)

0.29 cannot be stored exactly as a float64, and 0.29 * 100 comes out as 28.999999999999996. Converting a float to int in Go truncates toward zero, so that becomes 28, not 29. Some prices round up a hair (1.1 * 100 is 110.00000000000001) and survive, which makes the bug look random. Round to the nearest whole cent with math.Round before converting. Better still, keep money as integer cents from the start.

The pattern behind it: type errors.

More type errors to practise

Embed this challenge on your site

Paste this into any web page or blog post. Visitors fix the bug right there, in their browser.

<iframe src="https://trybughunt.com/embed/c8145e5a-1e5b-418d-b7d0-5e452efbcabc" title="Debugging challenge: Adding up 0.29 gives 28 cents" width="100%" height="580" loading="lazy" style="border:1px solid #d4d4d8;border-radius:12px;max-width:760px"></iframe>
<p style="font-size:14px">Debugging challenge from <a href="https://trybughunt.com/challenges/c8145e5a-1e5b-418d-b7d0-5e452efbcabc">TryBugHunt</a></p>