Why Python “%.2f” is not equal to Java “%.2f”? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why Python “%.2f” is not equal to Java “%.2f”?

Hi! Code challenge Kaleidoscopes (easy) In Python all test passed successfully, but in Java one test fails. In both programs the algorithm is same and I use System.out.printf(“%.2f”, price) for Java (price is double) and print(“%.2f” % price) in Python. When I use BigDecimals in Java for all my calculations I have got interesting result: I didn’t find the RoundingMode that suits all test! Almost all test passed when I use HALF_EVEN but one. And this one pass when I use DOWN rounding. Why?

29th Jan 2020, 12:30 PM
Jegors Čemisovs
Jegors Čemisovs - avatar
9 Answers
+ 6
HNNX 🐿, honestly spoken: I'm quite sure they didn't *plan* to trigger specific language intricacies. Surely it's just an accident. They wanted their new code coach feature and in many ways didn't think it through. You'll find several reports of users where it becomes quite obvious that the test cases are just sloppily designed.
29th Jan 2020, 6:00 PM
HonFu
HonFu - avatar
+ 3
I am guessing a bit here, but different languages seem to use slightly different rounding algos and get to different results. That came up in that paint cost code coach task before. May there be a relation? https://www.sololearn.com/discuss/2104980/?ref=app
29th Jan 2020, 1:12 PM
HonFu
HonFu - avatar
+ 2
I haven't done any manual rounding in my C version. I just output it with .2lf (implicit rounding) and it worked just fine.
29th Jan 2020, 1:30 PM
HonFu
HonFu - avatar
+ 2
Crazy. 🤨 They should really design their tasks and the tests in a way that they're unambiguous.
29th Jan 2020, 1:38 PM
HonFu
HonFu - avatar
+ 1
I haven't noticed anyone complaining this in java this might help: https://www.sololearn.com/Discuss/2135726/?ref=app //I have not attempted in java if you could tell me test nos of failing tests edit Jegors Čemisovs take a look in the link of diago's post
29th Jan 2020, 12:55 PM
Daljeet Singh
Daljeet Singh - avatar
+ 1
I writed the program that compare the correct calculation with sololearn calculation. The function small() is SoloLearn and big() is correct calculation. https://github.com/rabestro/sololearn-challenges/blob/master/easy/pro-kaleidoscopes/KaleidoscopesDifference.java And there is printed result for different values: https://github.com/rabestro/sololearn-challenges/blob/master/easy/pro-kaleidoscopes/KaleidoscopesDifference.txt
29th Jan 2020, 1:52 PM
Jegors Čemisovs
Jegors Čemisovs - avatar
+ 1
If I use incorrect calculation my programm pases all tests.
29th Jan 2020, 1:54 PM
Jegors Čemisovs
Jegors Čemisovs - avatar
+ 1
HonFu I've been following that thread of yours for some time now and really think they should reconsider "beginner" challenges which don't deal with logic but specific language intricacies. I struggled with rounding challenges because it worked on paper, the solution was right - I've even had test cases fail and succeed while running the same code twice.
29th Jan 2020, 2:57 PM
HNNX 🐿
HNNX 🐿 - avatar
0
What is array?
29th Jan 2020, 5:53 PM
111111
111111 - avatar