本文介绍了插入数据库时​​,双精度值0.0001将转换为1.0E-4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将从网页接收的double值传递给sqlServer数据库.值包装在值对象中.

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;
    }

}

但是,我遇到的问题是当值小于0.0001时,它将转换为 1.0E-4 或其他科学符号,从而导致数据库错误.

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.

我发现的方法是使用BigDecimal.valueOf(testVO.getValue())进行解析.

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!

推荐答案

双倍乘法可以得到意外的结果.检查以下代码.

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");
    }

结果将为Not Equal.正如Alberto Bonsanto所说,使用String保留在数据库中.我使用BigDecimal进行计算,并将其保留为字符串和(对于sqlserver为数字)保存在数据库中.

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.

这篇关于插入数据库时​​,双精度值0.0001将转换为1.0E-4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 22:43