原创

Spring Data 从数据库中的小数中删除“0”

温馨提示:
本文最后更新于 2024年04月12日,已超过 48 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

Searching in SO I've just seen questions about how to remove trailing zeros but what I wanna do is the contrary using Spring Data JPA.

I have the following data in my database with precio_venta and precio_coste being Decimal(10, 2):

enter image description here

And the following Entity in Spring:

@Data
@Builder
@Entity
@Table(name = "articulo")
public class ArticuloDAO {
    @Id
    @Column(name = "id")
    private int id;

    @Column(name = "nombre")
    private String nombre;

    @Column(name = "precio_venta")
    private Float precioVenta;

    @Column(name = "precio_coste")
    private Float precioCoste;

    @Tolerate
    ArticuloDAO() {}
}

But when I try to retrieve this data the prices got the last '0' removed which I don't want to happen, I want to keep them. This is the response I get and debugging I've seen that this happens from the moment I retrieve the data:

{
    "articulos": [
        {
            "id": 1,
            "nombre": "Botellín Mahou Clasica",
            "precioVenta": 1.5,
            "precioCoste": 0.44
        },
        {
            "id": 2,
            "nombre": "Botellín Mahou 5 estrellas",
            "precioVenta": 1.5,
            "precioCoste": 1.1
        },
        {
            "id": 3,
            "nombre": "Botellín Estrella Galicia",
            "precioVenta": 1.5,
            "precioCoste": 0.78
        }
    ]
}

How can I prevent this from happening?

Thanks!

正文到此结束
热门推荐
本文目录