That is an excellent article for explaining the concept.
I really appreciate you taking the time to look that up and share it. high five! :)
Try this ;-)
Also it should be 5 is not divisible by 2, corrected.
Thank you Voile for the reply, I'm really trying to wrap my head around this so I don't run into this problem twice.
I'm still confused, especially by the statement "2 is not divisible by 5". I'm not saying you are wrong, I just don't understand.
Could you please point me to a reference that explains this concept in greater detail? I've searched for an hour and can't find anything.
It's a natural phenomenom of floating point numbers because you cannot represent values with 2 decimal places exactly in floats (as 5 is not divisible by 2).
The test for 2.675 should've never been in there in the first place. I've removed it from the sample tests. The actual tests never have it anyway so you can just submit as normal ;-)
Fixed. For the record, sample test has always been free to change as it doesn't trigger a re-validation.
Also for 2.675 being rounded to 2.67, it's intended because of floating point error. If you want to handle floating point error properly, use the proper tools like a Decimal type.
This comment is hidden because it contains spoiler information about the solution
This kata, for at least Python, is harder than an Eight.
The testcase expected/actual diff should give you some clues. I'm guessing that using String.format that way causes the number to be rounded, instead of truncated toward zero.
What's wrong with my code
public class Numbers
public static double twoDecimalPlaces(double number)
return Double.valueOf(String.format("%.2f", number));
But when I writes this
return (int)(number * 100) / 100.0
It works. Why?
Python final tests are locked and can't be modified anymore. You can write a solution that passes the sample and the final tests if you want (it's possible).
Maybe that particular sample test could be removed to be consistent.
number_format returns a string to start with, read what it does here: http://php.net/manual/en/function.number-format.php
There is a better suited function you should use in php (and in most languages).
Some explain why this doesn't work for PHP?
return number_format($n, 2);
Python sample test is wrong. Final solution accepted when sample test fails.
or maybe there is a problem with the test case .. try "attempt"
This kata ...omg wth... my mind was blown. I'm not sure 8kyu for it. After into a tests was adding some new rules - such as 2.675? there is no sense for .format(), round() and other methods.