温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - Spring boot and BigDecimal or Double serialization problem
serialization spring-boot spring-mvc Thymeleaf bigdecimal

其他 - Spring Boot和BigDecimal或Double序列化问题

发布于 2020-04-26 16:44:11


İ无法正确处理BigDecimal初始化,我有一个具有BigDecimal字段的实体,如下所示:

@Entity
public class Menu{
...
   @Digits(integer=6, fraction=2)
   private BigDecimal price;
...
public BigDecimal getPrice() {
    return price;
}

public void setPrice(BigDecimal price) {
    this.price = price;
}
...
other generated getters and setters toString etc.
...
}

在前端,我使用html形式的thymeleaf(添加和更新新菜单项),并使用Big Decimal值输入:

<div class="form-group">
        <label for="price">Enter Price</label>
        <input type="number"  class="form-control" step="0.01" id="price" name="price" th:field="*{price}" placeholder="enter Price" value=" ">
</div>

为了限制用户在价格值上输入超过2位数的精度,我有这样的js代码:

$('#price').keypress(function (e) {
                var character = String.fromCharCode(e.keyCode)
                var newValue = this.value + character;
                if (isNaN(newValue) || parseFloat(newValue) * 100 % 1 > 0) {
                    e.preventDefault();
                    return false;
                }
            });

The question is : when user enters 10.25 value in the form , thymeleaf renders it like 10.2499998.. . Even other rest endpoints returns this entity with value in json like 10.249999. But when i looked at my database (postgresql and offcourse i use jpa) this value seems just fine which is 10.25. So i know when initializing new BigDecimal value the constructor must be like new BigDecimal("10.25") with string in parameter.But all i have is the default getters and setters and my controller is simple post controller for entity saving like this:

@PostMapping("/additem")
    public String addItemPost(@ModelAttribute(value="menu") @Valid Menu menu,Model model,BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return "addmenuitem";
        }
        this.menuService.saveMenuItem(menu);
        return "redirect:/restaurant/menu";
    }

The problem 2 is : I tried to change BigDecimal with Double value and after that, the input form takes value as we suggest and i see that value everywhere correctly.But if i write more digit after comma , the js function blocks me to write more than 2 digit but real value again becomes 10.2499999.. .
It must be something with spring's strategy of taking values from thymeleaf form, and than initialize those values into entity , but i never do that manually neighter couldn't debug it.
So ı don't know the problem is if my javascript function or my input form or the variable type.Please help. Any help will be appreciated.

查看更多

提问者
Berk Altuğ
被浏览
109
Berk Altuğ 2020-02-13 18:25

I found the problem and writing here if anyone encounter a problem like this. My Problem was not in localhost , but occured on heroku.So i realized that in heroku postgresql db my BigDecimal columns did not updated and in localhost they were changing whenever i re-run because of jdbc table creation strategy choice. I made the entity attribute Double and changed remote db columns to double precision than problem solved.