bigdecimal:零作为最后十进制的问题

发布于 2025-02-10 05:39:42 字数 1353 浏览 1 评论 0原文

我有一个春季启动应用程序,该应用程序消耗REST API,可提供一些小数点值 例如:

"Amount": {
  "Amount": 160.10,
  "Currency": "EUR"
}

我将其映射到bigdecimal,这给了我问题。我需要最后一个小数,即使是零,因为我需要没有小数的金额,即使用bigdecimal.unscaledvalue()

用于显示的数据,我期望x的输出。 getAmount()。unscaledvalue()“ 16010”,但是由于该值内部表示为160.1 (剥离了不合时宜的0),我得到<<代码>“ 1601” 是10倍折扣,当某些代码进一步划分除以10时。

该值可以具有0、2或3个小数位置(取决于货币)。

我的第一个想法是使用setScale()方法,但这给我带来了任何运气。

Java 11代码解析JSON:


restTemplate 
  .exchange(request, new ParameterizedTypeReference<List<NotificationMessage>>() {})
  .getBody()

型号:

import com.fasterxml.jackson.annotation.JsonRawValue;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import java.math.BigDecimal;

import lombok.Data;

@Data
@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy.class)
public class TransactionAmount {
//  @JsonRawValue //my test to force it
    private BigDecimal amount;

    private String currency;

}

有没有办法强迫Spring Boot保留不重要的小数?

(将API修改为使用字符串不是一个选项)

I have a Spring Boot application consuming a rest api delivering some decimal values
for example:

"Amount": {
  "Amount": 160.10,
  "Currency": "EUR"
}

I map this to BigDecimal which is giving me issues. I need the last decimal, even if it is zero, because I need the amount with no decimals, ie using BigDecimal.unscaledValue()

For the shown data I would expect the output of x.getAmount().unscaledValue() to be "16010" but since the value is internally represented as 160.1 (non-significant 0 is stripped) I get "1601" which is a factor of 10 off, when some code further down the line divides by 10.

The value can have either 0, 2 or 3 decimal places (depending on the currency).

My first thought was to use the setScale() method, but that brought me no luck.

Java 11 code to parse json:


restTemplate 
  .exchange(request, new ParameterizedTypeReference<List<NotificationMessage>>() {})
  .getBody()

model:

import com.fasterxml.jackson.annotation.JsonRawValue;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import java.math.BigDecimal;

import lombok.Data;

@Data
@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy.class)
public class TransactionAmount {
//  @JsonRawValue //my test to force it
    private BigDecimal amount;

    private String currency;

}

Is there a way I can force Spring Boot to preserve the non-significant decimals?

(modifying the API to use strings instead is not an option)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骑趴 2025-02-17 05:39:42

解决方案仅仅是为了引入自定义Jackson mapper将值解析为string而不是float

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Data;

import java.math.BigDecimal;

@Data
@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy.class)
public class TransactionAmount {

    private BigDecimal amt;

    private String ccy;
    
    // Custom mapper to make sure we get the complete decimal value - by treating the amount as a String instead of a Float
    @JsonCreator
    public TransactionAmount1(
      @JsonProperty("Amt") String amount, 
      @JsonProperty("Ccy") String ccy) {
        this.amt = new BigDecimal(amount);
        this.ccy = ccy;
    }

}

Solution was simply to introduce a custom Jackson Mapper which parses the value as String instead of float:

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Data;

import java.math.BigDecimal;

@Data
@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy.class)
public class TransactionAmount {

    private BigDecimal amt;

    private String ccy;
    
    // Custom mapper to make sure we get the complete decimal value - by treating the amount as a String instead of a Float
    @JsonCreator
    public TransactionAmount1(
      @JsonProperty("Amt") String amount, 
      @JsonProperty("Ccy") String ccy) {
        this.amt = new BigDecimal(amount);
        this.ccy = ccy;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文