Ruby/Rails - How can you validate against decimal scale?
The number of decimal places only matters when the value is a string. Once you've converted it to a number, it has however many decimals it has, zero or more.
So, the time to do this validation is on input (when it's a string, as all input from the user is always a string) and then enforce the correct display on output, when it is again a string. At all other times, you should not worry about the number of decimals.
In terms of Rails, you can use an accessor method which does some validation for you:
def my_column=(value)
if value[/\A\d+\.\d\d\z/]
write_attribute :my_column, value
else
errors.add(:my_column, :invalid)
end
end
You can't use a traditional validator as the string->decimal conversion will happen before your validator runs.
Validating decimal scale in Ruby on Rails
You could use the money gem, which I've used, or maybe try this rails wrapper for it which provides premade money validations.
Float validation on Rails
Well your precision and scale are off. Based on your description it should be (9,3) rather than (8,2).
Then the following should work:
class MyClass < ApplicationRecord
validates :my_float, numericality: {less_than: 1_000_000.0, greater_than_or_equal_to: 0.001}
end
This will validate that the number is less than 1 Million (6 digits to the left of the decimal) and greater than or equal to 0.001 three digits to the right of the decimal. If you need to handle negative numbers this could get a bit more interesting.
You could also possibly go with something like:
class MyClass < ApplicationRecord
validates :my_float, format: { with: /-?\d{1,6}\.\d{1,3}/}
end
Validate optional negative followed by 1-6 digits followed by a period followed by 1-3 digits.
Price Validation for Rails 4
I dont know which database you are using but you can define precision in your migration like this,
add_column :pieces, :price, :decimal, precision: 8, scale: 2
It will give you a total of 8 digits, with 2 after the decimal point.
About the validation,
If you want that the :price
should always have two decimal place (i.e: 4.99) you can try this,
validates :price, presence: true, format: { with: /\A\d+(?:\.\d{2})?\z/ }, numericality: { greater_than: 0, less_than: 1000000 }
If you want that the :price
should have at most two decimal or less (i.e: 4, 4.9, 4.99) you can try this,
validates :price, presence: true, format: { with: /\A\d+(?:\.\d{0,2})?\z/ }, numericality: { greater_than: 0, less_than: 1000000 }
Or if you dont want to validate precision and just want to round up the precision before you save it to the database you can use round_with_precision.
Validation of the maximum value of decimal is not activated properly
This is because you're using a decimal
type field and more chars are stored in the database field, here's a hint
instance.price = 123
instance.price.to_s # => '123.0'
You also specified a scale
of 4, that means that the number could have 5 characters after the integer part (4 digits and one dot).
Possibly what will fit your use case better is not length
, but less_than_or_equal_to
validation.
https://guides.rubyonrails.org/active_record_validations.html#numericality
Ruby on Rails - Validate a Cost
Check the price and verify the format
#rails 3
validates :price, :format => { :with => /\A\d+(?:\.\d{0,2})?\z/ }, :numericality => {:greater_than => 0, :less_than => 10}
#rails 2
validates_numericality_of :price, :greater_than => 0, :less_than => 10
validates_format_of :price, :with => /\A\d+(?:\.\d{0,2})?\z/
rails4: how to render error message when number has more scale digits than defined?
You can do this with the format
validation, giving it a regular expression:
validates :share_ksk, :central_office, :limit_value, :fix_disagio,
presence: true,
numericality: { less_than: 999.99, greater_than_or_equal_to: 0.00 },
format: { with: /\d*\.\d{2}$/, message: "must have two digits after decimal point" }
See: http://guides.rubyonrails.org/active_record_validations.html#format
Related Topics
What Is the Ruby Equivalent of the "This" Function in Java
Rugged Gem Installation Issue Possibly with Libgit2 on Windows
How to Make a Ruby Gem Package Copy Files to Arbitrary Locations
Argumenterror (Wrong Number of Arguments (Given 2, Expected 1)) After Updating to Ruby 3.0
Changing Http Status Message Using Sinatra
Ncurses to External Shell and Back Messing with Keys
Get Instance Variable Name from Itself in Ruby
Exclude Draft Articles from Solr Index with Sunspot
Using Ruby, What Is the Most Efficient Way to Get the Content Type of a Given Url
How to Run Perl and Ruby Scripts as Tasks in Ant
Creating a Gmail Draft with Recipients Through Gmail API
Installing MySQL-2.9.0 Gem on Windows Fails Due to Lack of Libmysql
Word Document.Saveas Ignores Encoding, When Calling Through Ole, from Ruby or Vbs
Ruby on Rails - Activerecord::Relation Count Method Is Wrong