Mybatis批量插入Java中的BigDecimal到SQL Server的decimal(18,2)时无法正确工作

huangapple go评论65阅读模式
英文:

Mybatis Batch insert Java BigDecimal to SQL Server decimal(18,2) not working correctly

问题

由于每个建议都在使用BigDecimal来避免精度问题。

我在Java中使用BigDecimal,与SQL Server列Decimal(18,2)一起使用。

当我使用new BigDecimal("37.99")插入值时,数据库中的值变成了38。

我还尝试过使用new BigDecimal(str).setScale(2, RoundingMode.UNNECESSARY),但仍然在数据库中得到38。

我应该如何避免这个问题?

英文:

Seeing that every suggestion us using BigDecimal to avoid the precision problem.

I'm using BigDecimal in java with the SQL Server column Decimal(18,2).

When I insert value with new BigDecimal("37.99") it turns out that the value in the database is 38.

I also tried using new BigDecimal(str).setScale(2, RoundingMode.UNNECESSARY) and still, get 38 in the database.

How should I avoid this problem?

答案1

得分: 0

我认为我找到了解决方法。

我正在使用myBatis批量插入来插入这个值,

而且其中一些值是0,我没有在程序中设置精度,

结果批量操作使用了最小精度来插入所有记录。

在我使用以下代码为每个BigDecimal值设置精度之后,

new BigDecimal("37.99").setScale(2)

数据库中的值变正确了。

希望有人能从这个答案中得到帮助。

英文:

I think I found the solution

I'm using myBatis batch insert to insert the value,

and some of the value is 0 and I didn't set the scale in the program

turn out the batch using the least scale to insert all record

after I set every BigDecimal value with

new BigDecimal("37.99").setScale(2)

the database value went correct.

Hope anyone could get help with this answer.

huangapple
  • 本文由 发表于 2020年10月8日 02:39:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64250348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定