当我需要使用浮点数(floats/doubles)时,如何在Spring中处理验证表单输入?

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

How exactly can I deal with validating form input in Spring when I need to use floats/doubles?

问题

我正在努力理解Spring Boot,所以我正在开发一个小的概率计算器应用程序。我的应用程序需要使用double或float来进行计算,但似乎Spring没有一种简单的方法来验证这些数据类型。现在,似乎我可以将“double”变量更改为“BigDecimal”,我尝试过了,但随后遇到了另一个问题,因为我正在使用对数,并且很快就明显,使用“BigDecimal”进行任何形式的计算都将导致大量不必要的代码。

难道真的没有简单的方法来验证double和float吗?

英文:

I'm trying to get my head around Spring Boot and so I'm working on a small probability calculator app. My application needs to use doubles or floats for its calculations and it seems like Spring does not have an easy way of validating these data types. Now, it seems that I could change the double variables into BigDecimal, I tried that, but then I ran into another issue because I am working with logarithms and it quickly became apparent that doing any kind of calculation with BigDecimal is going to lead to big chunks of unnecessary code.

Is there really no easy way to validate Doubles and Floats?

答案1

得分: 0

如果您想验证小数数值,您可以使用以下代码:

@DecimalMin(value = "0.1", inclusive = true)
@DecimalMax(value = "9.9", inclusive = true)
private BigDecimal measurement;
英文:

If you want to validate the decimal numbers then you can use the following code

@DecimalMin(value = "0.1", inclusive = true)
@DecimalMax(value = "9.9", inclusive = true)
private BigDecimal measurement;

huangapple
  • 本文由 发表于 2020年10月15日 12:18:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64364809.html
匿名

发表评论

匿名网友

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

确定