last day (14 days later) » 

4:31 AM
3
A: Hibernate @Digits Validation for Double type

bureaqueteYou've restricted the amount of digits for fraction part to 0 meaning no fraction allowed, even though there isn't any in your 0.0 but you should confirm that that is the case, I think you have a Double value that is 0 < x < 0.1 The value of the field or property must be a number within a spe...

 
Ok Noted bureaquete. Thanks for your answer. But if i give 10.0 instead of 0.0 with @Digits(integer=20, fraction=0). It getting pass.
 
@DEADEND I see, sorry for my answer, I hadn't tested or anything, when I test now all your cases are OK in my setup. I do not get any constraint violation for neither 0.0 nor 10.0, I think the trouble you have is how those values are being set into the bean, somehow you might be getting an approximate number such as 0.000000001 for 0.0 and might trigger the violation. Can you debug and confirm it?
 
private Double buildArea = 0.0; - Its fail. private Double buildArea = 10.0; - Its pass. Just i initialize like this and i have tested.
@ bureaquete hibernate-validator-4.1.0.Final. Please see my full code for your reference
 
@DEADEND nope, using your code directly, does not give any violation in my setup, can you also share your dependencies?
 
@ bureaquete javax.validation-1.0.0.GA - This is dependency jar.
 
4:31 AM
@DEADEND again same result, no violation, pretty weird, I cannot come up with any other reason.
 
@ bureaquete. I am using java 7. But same code and jar's give me a violation. Thank you so much for your effort bureaquete. Please let me know if you have reproduced or find any other solution.
 
@DEADEND What I know is, the validator logic gets the String representation of the double value you supply, and that is evaluated for @Digits restrictions, when you use String type for buildArea you can see this. I cannot find a reason for difference on your setup between 0.0 and 10.0, can you try using primitive double, and also float types & test again?
 
@ bureaquete. double 0.0 - Fail, float 0 - Fail, long 0 - pass
 
@DEADEND why use 0 for float? Haven't you used float buildArea = (float) 0.0 for it? And how could it fail for 0?
 
@ bureaquete float buildArea1 = (float) 0.0; - Fail. float buildArea1 = (float) 10.0; - Pass. float buildArea1 = 0; - Fail, double buildArea1 = 0; - Fail.
 
4:31 AM
Can you try this;
`Vehicle vehicle = new Vehicle();
System.out.println(vehicle.getBuildArea() % 1);`
before you pass to the validator, check the value of the buildArea field in your vehicle object
somehow it has fraction maybe? I cannot understand how this might be happening
 
Ok burea let me try that
Double buildArea = 0.0;

System.out.println(vehicle.buildArea % 1); >> 0.0
if i give like this
@Digits(integer=20, fraction=1)
Double buildArea= 0.0; Then no violation
 
Yeah but then that will accept 0.1 also
is it OK for your logic?
If you do not wish to have fraction at all, why not only accept only Long value?
Since Long will throw java.lang.NumberFormatException for wrong value
if it contains fraction
not a hibernate error, but at least something similar
but still not sure why the Double does not work, also the float, it should work, Can you also try Float? Not the primitive float but the wrapper class?
and initialize like this
private Float buildArea = new Float("0.0");
also try the same for the Double
private Double buildArea = new Double("0.0");
 
5:02 AM
@Digits(integer=20, fraction=0)
Float buildArea1 = (float) 0.0;
Violation occurs for this.
 
Oh, so it somehow fixed your issue, however I couldn't find the reason for problem with Double at least you got a solution :)
Should I update my question suggesting using Float, and if you'd like you can accept it
 
null, 0, 0.0, 10.0, 10.10 - These should be pass. if exceeded 20 digit - is should be fail
for double data type
This is my requirement
 
how is 10.10 ok?
you said zero fraction
 
Sorry, i should increase fraction to 2
 
hmm, the whole question has changed now lol
 
5:08 AM
Should I update my question suggesting using Float, and if you'd like you can accept it
double is convenient for amount, square feet and all right
is it right to use float
 
Can you try to update your fraction to 2, then try to run again with 0.0, then you would get no violation I think
Yeah Double is better of course, but you have weird troubles, with this new change, your question might be meaningless, since it might not have any problem now
 
Yes no violation
 
with fraction = 2
yeah, you can delete your question I think lol
how about
0.00 works?
and 0.000
 
No my question is how should work 10.0 but working for 0.0 when using fraction 0
 
so for that case, I can suggest Float since it resolved your problem, but I have no other ideas why that is happening
I've updated my answer, up to you to accept it or not, I know it is not the perfect solution, but there is no logical reason comes to my mind why that happens to you, but I cannot reproduce the issue
 
5:25 AM
Ok burea Thank you so much for your effort.
i accepted the answer
@DecimalMin & @DecimalMax also working
 

last day (14 days later) »