Kotlin Double | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Kotlin Double

Well, if you know, the shipping task after quiz #3 is kinda weird for me, just because: Expected output: 29.945 (forgot the number, but it won't affect me to explain) My output: 29.945000000003 How to round the number to 3 decimals after a dot? Is there in Kotlin some functions for this?

16th Nov 2021, 9:12 AM
SelfyTheMentalist
SelfyTheMentalist - avatar
5 Answers
+ 7
It's a popular issue in the most of the programming languages. This topic is different but same concept. Take a look at this thread https://www.sololearn.com/Discuss/1288636/?ref=app
16th Nov 2021, 11:50 AM
Simba
Simba - avatar
+ 6
var num: Double = String.format("%.3f", 29.9450000003).toDouble() println(num) But, I think we don't need to use this for that task. Can we see your code?
16th Nov 2021, 11:25 AM
Simba
Simba - avatar
+ 5
Instead of a/100*15 try a*15/100 to avoid these kind of floating points errors.
16th Nov 2021, 11:38 AM
Simba
Simba - avatar
16th Nov 2021, 11:27 AM
SelfyTheMentalist
SelfyTheMentalist - avatar
0
Simba why do they appear?
16th Nov 2021, 11:39 AM
SelfyTheMentalist
SelfyTheMentalist - avatar