Skip to content
Home » Never Pass a Double as Argument When Constructing BigDecimal Objects in Java

Never Pass a Double as Argument When Constructing BigDecimal Objects in Java

lots of numbers

See what happens when you pass a double as argument when constructing BigDecimal objects in Java.

import java.math.BigDecimal;

public class DoubleAsBigDecimalConstructorArgument {

    public static void main(String[] args) {
        // Prints 1.229999999999999982236431605997495353221893310546875
        System.out.println(new BigDecimal(1.23));

        // Prints 1.23
        System.out.println(new BigDecimal("1.23"));

        // Prints 1.23
        System.out.println(BigDecimal.valueOf(1.23));
    }
}

If you have any questions, leave a comment or ask me on my social media.