Will
Will

Reputation: 211

double value 0.0001 will converto 1.0E-4 while inserting into database

I would like to pass the double value received from web page to sqlServer database. the value are wrapped in a value object.

public class testVO{
    Double value;

    public Double getValue() {
        return value;
    }

    public void setValue(Double value) {
        this.value = value;
    }

}

However, the problem I've met is when the value is smaller than 0.0001, it will transfer into 1.0E-4 or other scientific-notation, which causes the database error.

The way I found is to use BigDecimal.valueOf(testVO.getValue()) to parse it.

Is there any other better way to solve it?


Solved: It's not because of scientific-notation problem, Database can read both number and scientific. The problem is the utility package I used while parse it to database will convert all value to string type, hoping don't waste your time!

Upvotes: 2

Views: 8833

Answers (1)

Zaw Than oo
Zaw Than oo

Reputation: 9935

Double Multiplication can get the unexpected result. Check below code.

    double a = 0.3;
    double b = 0.1 + 0.1 + 0.1;
    if(a == b) {
        System.out.println("Equal");
    } else {
        System.out.println("Not Equal");
    }

The result will be Not Equal. As Alberto Bonsanto said, use String to keep in database. I use BigDecimal for calcuation process, and keep it as string and (numeric for sqlserver) in database.

Upvotes: 1

Related Questions