英文:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论