Search code examples
javamysqldecimalbigdecimal

When I insert 78.9 into Mysql (using JDBC) it gets rounded up to 79? Is this normal


When I insert 78.9 into Mysql (using JDBC) it gets rounded up to 79? Is this normal ... if so how can I stop this from happening.

More details:

Column name: num Data type: decimal(12,0) * The above item was copied from phpMyAdmin

Query is

stmt.executeUpdate("INSERT INTO triples(sub_id, pro_id, num) VALUES("+subId+","+proId+",78.9)");

Ideally I would use a variable instead of the hard-coded 78.9

Such as

        BigDecimal obj = new BigDecimal(78.9);

Solution

  • You need to set the datatype of the column you are inserting into as

    float(x,y)
    

    or

    decimal(x,y)
    

    where x is the total number of digits and y is the total number of decimals.

    e.g. float(5,2)  -> 325.46
         decimal(10,5) -> 42579.12345