0

help please my rounding off Counting Method ()

I created 5 functions to give me proper change sometimes it gives me proper change others times it won't example 1.88 should give 1 dollars,3 quarters, 1 dimes 0nickles 3 pennies instead it gives me 1 dollars 3 quarters 1 dimes 1 nickles 3 pennies it seam like my rounding is off also if you can help me plurise it that would be greatly appreciated. fun main(args: Array<String>) { var change = 1.88 println("" + dollars(change).toInt() + " dollars") println("" + quarters(change).toInt() + " quarters"); println("" + dimes(change).toInt() + " dimes"); println("" + nickles(change).toInt() + " nickles"); println("" + pennies(change).toInt() + " pennies"); } fun dollars(totalDollars: Double): Double { return Math.floor(totalDollars / 1) } fun quarters(totalQuarters: Double):Double { return ((totalQuarters % 1) / .25).toDouble(); } fun dimes(totalDimes: Double): Double { return Math.round(totalDimes % 1 % .25 / .10).toDouble(); } fun nickles(totalNickles: Double): Double { return Math.round(totalNickles % 1 % .25 % .10 / .05 ).toDouble(); } fun pennies(totalPennies: Double): Double { return Math.round(totalPennies % 1 % .25 % .10 % .05 / .01).toDouble(); }

14th Jun 2022, 2:35 AM
Mikey
6 Respostas
+ 2
I have some suggestions to improve your code. You should be consistent with Math.floor everywhere, maybe Math.round is causing your rounding issue. When you deal with currency, it is never a good idea to use Double, because it is not precise. Eventually you will hit some rounding issue, and in the monetary world it is unacceptable to lose precision. In this case you could get away with using Int and just work with penny amounts, rather than dollars as Double. This helps a lot with rounding (Int division already rounds down) and also with output. But in general, you should use BigDecimal type for money. Many operations you do are redundant and not necessary, for example: X / 1 is the same as X X % 1 % 0.25 is the same as X % 0.25 Math.round() already returns a Double so you don't need to convert to Double again These make your code more complicated to understand. KISS ;) Use string interpolation for printing, better Kotlin style. My version is this: https://code.sololearn.com/cm5VP31T8Vb4/?ref=app
14th Jun 2022, 4:51 AM
Tibor Santa
Tibor Santa - avatar
+ 1
thank you!! Will that still work with five functions? the the instructor wants me to Create five separate functions dollars(), quarters(), dimes(), nickels() and pennies(). Each function will - accept a double amount as an argument its becoming hard because we get one pre-recorded lecture that dates back to 2019 and one quiz then told to do an assignment and its hard to get a hold of him :(
14th Jun 2022, 5:07 AM
Mikey
+ 1
Sure if this is the task then you can create 5 separate functions. But I would make each of them return an Int value instead of Double. Because it makes no sense to have 3.14 nickels right? :) fun nickels(total: Double): Int = (total * 100).toInt() % 25 % 10 / 5
14th Jun 2022, 5:12 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Thank you I’ve been trying to use that code with the five functions() and its not working I think im over thinking it 😩
14th Jun 2022, 10:01 AM
Mikey
+ 1
Side note: get used to indent your code. It makes it way more readable, then more mantainable, and more prone to help from others.
14th Jun 2022, 11:31 AM
Emerson Prado
Emerson Prado - avatar
0
This is what I was able to produce similar issue is just on a different number what was that decimal to double thing you mentioned? fun main() { var change = 11.24 println("" + dollars(change).toInt() + " dollars") println("" + quarters(change).toInt() + " quarters"); println("" + dimes(change).toInt() + " dimes"); println("" + nickles(change).toInt() + " nickles"); println("" + pennies(change).toInt() + " pennies"); } fun dollars(totalDollars: Double):Int { return (totalDollars * 100/100).toInt() } fun quarters(totalQuarters: Double):Int { return (totalQuarters*100).toInt() % 100 / 25 } fun dimes(totalDimes: Double):Int { return (totalDimes * 100).toInt() % 25 / 10 } fun nickles(totalNickles: Double): Int { return (totalNickles * 100).toInt() % 25 % 10 / 5 } fun pennies(totalPennies: Double): Int { return (totalPennies *100).toInt() % 5 / 1 }
14th Jun 2022, 1:24 PM
Mikey