[BUG] Multiplication of float returns incorrect value

I wrote a simple multiplication snippet. for 8.8 * 9 it should return 79.2 but it returns 79.200005

import java.util.*

fun main(args: Array<String>) {
    println("Hello World!")

    val reader = Scanner(System.`in`)
    print("Enter a number: ")

    var integer1= reader.nextFloat()
    print("Enter another number: ")
    var integer2 = reader.nextFloat()
    //val integersum: Float = integer1 * integer2
    println("Your product: ${integer2*integer1}")

OUTPUT:

Hello World!
Enter a number: 8.8
Enter another number: 9
Your product: 79.200005

It’s expected behavior for floating point numbers: Is floating point math broken? - Stack Overflow If you need 100% precision, you can try BigDecimal.

1 Like