• Post Reply Bookmark Topic Watch Topic
  • New Topic

Increment Validator?  RSS feed

 
andy armstrong
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know of a open source utility to validate increments on a number.
For example if only increments of .25 were allowed then the input
1.30 would fail... but 1.75 would pass.
Cheers,
Andy
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need an open source utilty, you simply need to use the % (modulus) operator, which returns the remainder when the first number is divided by the second:

Therefore, 1.30 % .25 returns 0.050000000000000044 (doubles are unfortunatley prone to rounding errors); that does not equal 0.0, so the functions returns false
1.75 % .25 does equal 0.0, so the function returns true.
To get around the rounding errors problem, you might want to set up a tolerance threshold; that is if (mathemetically speaking, not Java speaking) -0.000000001 < result < 0.00000001 is true then return true....
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Watch out for floats!
[ August 13, 2003: Message edited by: Cindy Glass ]
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh Joel, You beat me!!
I walked off to a meeting and just finished this up, and didn't notice that you already responded <sigh>.
 
andy armstrong
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks that is good . I am using a big decimal so I will implement and
set up the thresholds appropriately.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!