我是靠谱客的博主 留胡子雪糕,最近开发中收集的这篇文章主要介绍java 1.0e-4,插入数据库时​​,双精度值0.0001将转换为1.0E-4,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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!

解决方案

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.

最后

以上就是留胡子雪糕为你收集整理的java 1.0e-4,插入数据库时​​,双精度值0.0001将转换为1.0E-4的全部内容,希望文章能够帮你解决java 1.0e-4,插入数据库时​​,双精度值0.0001将转换为1.0E-4所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(71)

评论列表共有 0 条评论

立即
投稿
返回
顶部