I'm in the process of setting up a chemical model that takes inputs as mols, but discretely simulates the molecules (i.e., each molecule is one entity). As a result, I need to be able to calculate out the proportions as whole numbers. For example if I have 0.01 mols of A and 0.002 mols of B I would scale that out to 10 units of A and 2 units of B. However, this code is pretty inelegant and assumes that all molar counts will be less than 1.0, i.e.,
// Find the exponent to offset the value
NumberFormat format = new DecimalFormat("0.#E0");
String value = format.format(smallest);
int exponent = Integer.parseInt(value.substring(value.indexOf("E") + 1));
// Scale to whole numbers
exponent = Math.abs(exponent) + 1;
for (int ndx = 0; ndx < input.size(); ndx++) {
input.get(ndx).count = (long)(input.get(ndx).mols * Math.pow(10, exponent));
}
Is there an efficient algorithm to perform decimal shift on arbitrary inputs (ex., 1.1 mols of A, 0.11 mols of B, 0.001 mols of C befomes 1100 units of A, 110 units of B, and 1 unit of C)?