I'm considering writing a small java class for handling decimals. The basic idea would be to have two int
fields, one for holding the value to the left of the decimal point, and the other would hold the value to the right of the decimal. E.g in the following number:
100.50
100 would be in the first int, and 50 would be in the 2nd.
Is that worth doing, or does BigDecimal
already do something like this in its implementation?
A good rule of thumbs is (paraphrased from J. Bloch, Effective Java, 2nd Ed.):
"Never reinvent the wheel. If there is a Java API class that can be used to do what you want, then use it rather than trying to rewrite your own class. Language developers have been working for years to refine and optimize the libraries, often in ways that would be impractical for independent developers (and continue to optimize them)."
It may be that your needs are simple. For example, perhaps you'll just be using this number to identify an object (like the Dewey Decimal System) and you'll never be doing numeric operations on them. If your needs are this simple (you haven't told us what you'll be doing with these numbers), then it may be sensible to write your own class as an optimization.
However, if you're going to be doing any kind of math on these numbers, then using BigDecimal to represent a numeric quantity with fixed precision is absolutely the right way to go. I wouldn't even worry about the memory. Memory is cheap.
Consider also that BigDecimals already: