Displaying 20 results from an estimated 1000 matches similar to: "Form entries with decimals generating float errors"
2006 Jun 07
1
Setter that converts a float attribute to integer
Hi,
I have some problems with an application where I''m using custom
accessors to do currency conversions. In my model, I have a price
attribute in the database that stores the value in cents, to avoid
future problems with float arithmetic and round. But at the views, I
would like to show the price in euros, with decimal for the cents. So
I defined a new attribute called price_in_euros,
2006 Jan 17
9
Formatting a float with a set number of decimals
Another newbie question:
How do i convert a float to a string, rounded to a certain number of
decimals?
Thanks
--
Posted via http://www.ruby-forum.com/.
2006 Jun 28
3
how do I validate currency format if I am storing in cents?
Hi all -
To avoid floating point issues, I''ve decided to store monetary values in
cents in the database. However, the user will enter these in dollars
and cents. Two questions:
1) How do I do the validation for the currency format? It looks like
ActiveRecord truncates the cents since it thinks the field type is a
Fixnum. Am I forced to do validation in the controller?
2) Where
2006 Mar 03
4
DB data type enforcement in Active Record
I have a question about how ActiveRecord handles data types.
When I enter text in a text_field (meaning, a field in the GUI) which
belongs to a numeric field in the database, Active Record automaticly
converts it to 0, because that''s what the to_f/to_i method of a string
does. Is it also possible to have Active Record enforce the types, so
that when you enter text for a numeric
2006 Jun 23
6
float calculation error
Hi
I have the following values
pActualCost = 33.00
pPaymentCost = 29.99
So this calculation should leave me with 0.01
pPaymentDifference = pActualCost - pPaymentCost
however when doing this in rails, it returns 0.00999999999999801
has anyone got any suggestions to whats going wrong and how I can
correct this
Thanks
Scott
--
Posted via http://www.ruby-forum.com/.
2008 Mar 19
2
ActiveRecord bug? ActiveRecord::ConnectionAdapters::Column.type_cast for float columns
Hi all,
I just noticed that in ActiveRecord::ConnectionAdapters::Column.type_cast[1]
(and type_cast_code[2]) the type casting for float columns simply does
value.to_f, whereas all the others have a rescue or call out to a method,
the default implementations of which have rescues or enough logic in that I
don''t think they''d fail.
Imagine the following:
create_table :vehicle do
2006 May 08
1
How to check if a given string is a valid float value or not
I can''t use active record or anything. I need to validate inside the
controller. I thought I could use to_f in the string method. The problem is
that for a value of 1.3fsfs it returns 1.0 . I thought it would be
consistently 0.0 . I can get a hack around it but looking for an elegant
solution.
Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
2006 Jan 19
7
validates_numericality_of positive integer
Hi,
What is the simplest way to validate a positive integer?
validates_numericality_of :foo, :integer_only => true
how do I add the positive part? Do I need another validation statement
for pattern matching or do I have to write a validate() funciton for
my model?
Thanks,
Peter
2006 Jun 07
2
Problem with a setter that converts euros to cents
Hi,
I have some problems with an application where I''m using custom
accessors to do currency conversions. In my model, I have a price
attribute in the database that stores the value in cents, to avoid
future problems with float arithmetic and round. But at the views, I
would like to show the price in euros, with decimal for the cents. So
I defined a new attribute called price_in_euros,
2006 Apr 03
4
validates_numericality_of, :allow_nil => true?
Is there a way to use validates_numericality_of and still allow null
values?
I have a model with some optional values that can be nil, but if they''re
present, they must be numbers. Will I need to construct a custom
valiation for this, or is there some method built in? Seems like it
would be a common enough need.
Jeff
--
Posted via http://www.ruby-forum.com/.
2005 Dec 15
8
slightly OT - Ruby division
Hey all,
In my code, it seems that when I divide two integers, the result is an
integer. Is there any way to make it such that when I divide two integers,
the result is a double?
Any help appreciated,
Jin
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
2006 Sep 13
4
Mini_Magick Problem
I can''t for the life of me figure this out. Been banging my head on
this since the weekend, and can''t see what I''m doing wrong.
This code works:
image.resize("640x480")
image.write(path)
I run this and I get the resized file just fine.
This doesnt work at all:
width = 200
height = 100
dimensions = "#{width}x#{height}"
2005 Oct 28
4
find_by_sql column types
Hello--
There must be a better way to do this. I have a class method in my model
that finds averages and does a few calculations using find_by_sql. The
problem I¹m encountering is that all computed columns from MySQL come back
as type string. E.g.,
def self.find_averages(domain_id)
if @@domain_average
return @@domain_average
else
@@domain_average =
2008 Nov 11
3
FXTextField and number of digits after decimal point
Hello,
Here''s the context:
I read a number either pure integer or float with eventually up to 12
digits after the decimal point from a YAML file.
The number is injected into a FXTextField with the following code:
@value_sel = FXDataTarget.new(@value.to_s)
@value_value = FXTextField.new(@matrix_mem_value, 20, at value_sel,
:opts =>TEXTFIELD_NORMAL|JUSTIFY_RIGHT,
2008 Apr 04
6
validates_numericality_of :not_equal_to
I have a situation in which I want to validate a number is not zero.
Both positive and negative values are acceptable, just not zero.
validates_numericality_of has an :equal_to attribute, but I don''t see a
:not_equal_to. I have not yet been able to find anyone who has
discussed this. I did find information about custom validators, so I am
now using a custom validator to check the field
2006 Mar 21
13
Double and single quote usage in AWDWR
Hello everyone!
I''ve noticed, that the book uses double quotes almost everywhere. It is like
<%= javascript_include_tag "prototype" %>
I''ve checked the "programming ruby, 2ed" book and found that double
quotes are most suited when escape sequences and substitutions are
required. Isn''t it more semantically correct to write this code like
2006 Sep 25
3
Engine Yard blog
Just received the news from Tom Mornini.
Congrats Ezra for the new Engine Yard site and the blog you will be
collaborating.
Hope to read you there soon.
http://www.engineyard.com/
Jonathan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/backgroundrb-devel/attachments/20060925/3f251fa4/attachment.html
2006 May 04
3
Decimal
Can someone please tell me the best approach to adding support for
editing MySQL decimal types in rails?
I have a field in the DB that represents currency, and the view will not
let save anything past the decimal place so, 10.99 becomes 10.00
automatically.
Any help is appreciated.
--
Posted via http://www.ruby-forum.com/.
2011 Feb 09
4
Modules being shared and model data
Have in a module in ./lib:
module Pricing
def sell_price
(cost.to_f)/((100-(margn.to_f))/100)
end
def sell_price_inc
((product.cost.to_f)/((100-(product.margn.to_f))/100)*1.14)
end
def charge_excl
(sell_price).to_f * quantity
end
end
A model product:
class Product < ActiveRecord::Base
include Pricing
attr_accessible :name, :code, :cost, :company_id, :margn
2006 Apr 02
16
12 / 16 = 0
Hi,
Just thought it was weird that Ruby doesn''t automatically cast ints to
floats. Seems so un-rubyish
irb(main):006:0> 12/16
=> 0
irb(main):007:0> 12.to_f / 16.to_f
=> 0.75
Jeroen