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

穿精又带淫゛_ 提交于 2019-12-11 02:38:50

问题


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!


回答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.



来源:https://stackoverflow.com/questions/25027467/double-value-0-0001-will-converto-1-0e-4-while-inserting-into-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!