Skip to content

Commit

Permalink
Money calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperancinha committed Oct 29, 2023
1 parent 8d5ce99 commit 2938fde
Showing 1 changed file with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.jesperancinha.ktd

class FloatVsDouble {
companion object {
@JvmStatic
fun main(args: Array<String>) {
val floatNumber = 7.3333333333333333333333333333333f
println(floatNumber)
println("The number $floatNumber has ${floatNumber.toString().length - 1} digits in precision")
val doubleNumber = 7.3333333333333333333333333333333
println(doubleNumber)
println("The number $doubleNumber has ${doubleNumber.toString().length - 1} digits in precision")
val bigDecimal = "7.3333333333333333333333333333333".toBigDecimal()
println("The number $bigDecimal has ${bigDecimal.toString().length - 1} digits in precision")

val value1 = 9
val value2 = 9

println("This is the result of a bad idea sum ${badIdeaSum(value1, value2)}")
println("This is the result of a good idea sum ${goodIdeaSum(value1, value2)}")
}

/**
* This function will try to sum
* value1,000,000,000,000,000,000
* with
* value2,000,000,000,000,000,000
* correctly
*/
private fun goodIdeaSum(value1: Int, value2: Int): String {
return ((value1 + value2).toBigDecimal().multiply(1_000_000_000_000_000_000.toBigDecimal() )).toString()
}

/**
* This function will try to sum
* value1,000,000,000,000,000,000
* with
* value2,000,000,000,000,000,000
* using a terrible idea
* NOTE that this idea is defended by some interviewers in selection processes.
* Do not feel bad about it if the interviewer rejects your application because of this.
* You probably wouldn't want to work in a place with these criteria any way.
*/
private fun badIdeaSum(value1: Int, value2: Int): String {
return ((value1 + value2) * 1_000_000_000_000_000_000).toString()
}
}
}

0 comments on commit 2938fde

Please sign in to comment.