I’m finding that i’m needing to compute large numbers to high precision. For example: 5422300.8452. What is the best way to store this data so that simple arithmetic operators can act on it? I’ve been trying to do combinations of Longs and Doubles but it gets complicated after a significant number of computations.
I’m sure there is a simple solution, i’m just not so knowledgeable yet.
Ok Basically, I figured it out:
My problem was that I wanted to keep 4 decimal places of precision but entering a high number started truncating digits. For example, I had this:
Double Remain = (double) Math.round((double quantity/12)*10000)/10000;
This works for smaller numbers but 900000000 still gets truncated to 7.5 something.
So I basically need to divide the numbers as long
and then take the modulus of the 2 and divide it by the denominator, and round the result.
Then add the 2.
It works!
Ok Basically, I figured it out:
My problem was that I wanted to keep 4 decimal places of precision but entering a high number started truncating digits. For example, I had this:
This works for smaller numbers but 900000000 still gets truncated to 7.5 something.
So I basically need to divide the numbers as long
and then take the modulus of the 2 and divide.
Perhaps it would’ve helped if explained the purpose of what I was trying to do. I wanted the code to display a specific number of significant digits. If I use a Double as the variable to store user input to be divided, many digits might get truncated (depending on the number of resulting decimal places.) I needed a way to standardize the scope of the variable in the whole numbers while keeping the numbers precise to 4 decimal places.
In other words, I need to be able to handle the same size number regardless of how many decimal places the quotient takes.
In order to do this I take the user input, and divide it by the literal (12).
Then I take the modulus the same way and divide the modulus by 12.
I then piece the 2 together like so: