An import file gives you an amount as text, but its destination expects an integer count of minor units. Multiplying a double and casting to long gives the conversion two ways to lose information. Parse decimal text directly, shift by the destination scale, and require an exact integer at the boundary.

Make the conversion fail on information loss

Use JDK 21 and save the following as MinorUnits.java. Pass the scale explicitly from the destination schema. The example limits scales to 0 through 9 as an application rule, not a restriction of BigDecimal. It allows signed values so refunds can use the same conversion.

import java.math.BigDecimal;

public class MinorUnits {
    static long minor(String decimal, int scale) {
        if (scale < 0 || scale > 9)
            throw new IllegalArgumentException("unsupported scale");
        return new BigDecimal(decimal).movePointRight(scale).longValueExact();
    }

    static void rejected(String input) {
        try {
            minor(input, 2);
            throw new AssertionError("accepted: " + input);
        } catch (ArithmeticException expected) {
            // Fractional minor unit or outside the long range.
        }
    }

    public static void main(String[] args) {
        long first = minor("12.340", 2);
        long refund = minor("-0.05", 2);
        long whole = minor("42", 0);
        if (first != 1234 || refund != -5 || whole != 42 ||
            minor("92233720368547758.07", 2) != Long.MAX_VALUE)
            throw new AssertionError("wrong conversion");
        rejected("12.345");
        rejected("92233720368547758.08");
        System.out.printf("%d %d %d%n", first, refund, whole);
    }
}

Compile and run from the directory containing the file:

javac --release 21 MinorUnits.java
java MinorUnits

Expected output:

1234 -5 42

Distinguish precision from spelling

12.340 is accepted at scale two because the extra zero does not represent a fraction of a minor unit. 12.345 is rejected because it would become 1234.5. The test also checks the largest representable long and the next minor unit, which must fail instead of wrapping around. If you need to reject three fractional digits even when the last is zero, enforce that as a separate input-format rule before conversion.

Troubleshoot imports

If a row produces NumberFormatException, inspect the input format first. This parser expects decimal text, not a formatted currency label with grouping separators. BigDecimal can accept exponent notation, so restrict the accepted text separately if your file format forbids it. If the conversion produces ArithmeticException, identify the field and check the source scale and target range. Do not retry with a lossy cast. If your specification calls for rounding, apply its named rounding rule before the exact conversion and cover half-way cases in tests. For amounts coming from several schemas, keep the scale with the unit instead of guessing it from the number of digits in a value.

Primary reference and related tutorials: JDK 21 BigDecimal API · Java Samples Java tutorials.