2

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!

Will
  • 221
  • 3
  • 12

1 Answers1

1

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.

Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131
  • thanks! by the way, could you explain more about "keep it as String and numeric for sqlserver in database". Do you mean you save as numeric type in sqlSever and string in other database? and why? – Will Jul 30 '14 at 05:04
  • @Will, when I use sqlserver, I use `numeric`. It is ok for me. Other database, I did not use it yet. I think, there might be datatype like `numeric`. For example `number`. If you use `String`, it is ok. it might need programmatically convert `String` to `Number` vise visa. – Zaw Than oo Jul 30 '14 at 05:13