Ruby integer always rounds to the nearest whole number and won't save decimal places
up vote
0
down vote
favorite
I have a simple form with an input for fee
which is an integer. I want the input to be stored like money/prices are normally - to 2 decimal places - regardless of the input. If someone enters 5.1
into the form, I'd like it to show 5.10
in my view, for example. At the moment if I enter 5.1
it shows 5
. Regardless of what is entered into the form a whole number is returned.
Here is the _form code:
<%= simple_form_for @order do |f| %>
<%= f.input :start_point %>
<%= f.input :restaurant_location %>
<%= f.input :customer_location %>
<%= f.input :fee %>
<%= f.button :submit %>
<% end %>
and here is the code from my index view:
<% @orders.each do |order| %>
<h4 class="order-info"><%= order.start_point %></h4>
<h4 class="order-info"><%= order.restaurant_location %></h4>
<h4 class="order-info"><%= order.customer_location %></h4>
<h4 class="order-info"><%= order.fee %></h4>
<%= link_to "Edit", edit_order_path(order) %>
<%= link_to "Delete", order_path(order), method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>
If I write it as '%.2f' % order.fee
it shows 5.00
in the view instead of 5.10
ruby-on-rails ruby
|
show 3 more comments
up vote
0
down vote
favorite
I have a simple form with an input for fee
which is an integer. I want the input to be stored like money/prices are normally - to 2 decimal places - regardless of the input. If someone enters 5.1
into the form, I'd like it to show 5.10
in my view, for example. At the moment if I enter 5.1
it shows 5
. Regardless of what is entered into the form a whole number is returned.
Here is the _form code:
<%= simple_form_for @order do |f| %>
<%= f.input :start_point %>
<%= f.input :restaurant_location %>
<%= f.input :customer_location %>
<%= f.input :fee %>
<%= f.button :submit %>
<% end %>
and here is the code from my index view:
<% @orders.each do |order| %>
<h4 class="order-info"><%= order.start_point %></h4>
<h4 class="order-info"><%= order.restaurant_location %></h4>
<h4 class="order-info"><%= order.customer_location %></h4>
<h4 class="order-info"><%= order.fee %></h4>
<%= link_to "Edit", edit_order_path(order) %>
<%= link_to "Delete", order_path(order), method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>
If I write it as '%.2f' % order.fee
it shows 5.00
in the view instead of 5.10
ruby-on-rails ruby
What do you mean "fee which is an integer"? Do you mean your model has afee
field that is of the typeinteger
?
– jvillian
Nov 15 at 1:55
fee
is just the name of the column in the table, with datatype integer. Sorry if i'm not using the correct terminology - i'm relatively new to programming.
– Steve
Nov 15 at 2:00
If the data type is integer, then you can only store an integer in it. If you want to store something else, then you'll need to change the data type. If you're using it to store money-like values, then consider a Big Decimal. You can Google around for the pros and cons of using different data types to store money values.
– jvillian
Nov 15 at 2:07
what datatype should be used for money?
– Steve
Nov 15 at 2:09
1
Just to be clear: It is fully possible and quite common to store monetary values as integers in databases. The values are stored in the type of currency (e.g. cents), so storing3.05$
becomes305
in the database.
– Marcin Kołodziej
Nov 15 at 2:56
|
show 3 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a simple form with an input for fee
which is an integer. I want the input to be stored like money/prices are normally - to 2 decimal places - regardless of the input. If someone enters 5.1
into the form, I'd like it to show 5.10
in my view, for example. At the moment if I enter 5.1
it shows 5
. Regardless of what is entered into the form a whole number is returned.
Here is the _form code:
<%= simple_form_for @order do |f| %>
<%= f.input :start_point %>
<%= f.input :restaurant_location %>
<%= f.input :customer_location %>
<%= f.input :fee %>
<%= f.button :submit %>
<% end %>
and here is the code from my index view:
<% @orders.each do |order| %>
<h4 class="order-info"><%= order.start_point %></h4>
<h4 class="order-info"><%= order.restaurant_location %></h4>
<h4 class="order-info"><%= order.customer_location %></h4>
<h4 class="order-info"><%= order.fee %></h4>
<%= link_to "Edit", edit_order_path(order) %>
<%= link_to "Delete", order_path(order), method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>
If I write it as '%.2f' % order.fee
it shows 5.00
in the view instead of 5.10
ruby-on-rails ruby
I have a simple form with an input for fee
which is an integer. I want the input to be stored like money/prices are normally - to 2 decimal places - regardless of the input. If someone enters 5.1
into the form, I'd like it to show 5.10
in my view, for example. At the moment if I enter 5.1
it shows 5
. Regardless of what is entered into the form a whole number is returned.
Here is the _form code:
<%= simple_form_for @order do |f| %>
<%= f.input :start_point %>
<%= f.input :restaurant_location %>
<%= f.input :customer_location %>
<%= f.input :fee %>
<%= f.button :submit %>
<% end %>
and here is the code from my index view:
<% @orders.each do |order| %>
<h4 class="order-info"><%= order.start_point %></h4>
<h4 class="order-info"><%= order.restaurant_location %></h4>
<h4 class="order-info"><%= order.customer_location %></h4>
<h4 class="order-info"><%= order.fee %></h4>
<%= link_to "Edit", edit_order_path(order) %>
<%= link_to "Delete", order_path(order), method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>
If I write it as '%.2f' % order.fee
it shows 5.00
in the view instead of 5.10
ruby-on-rails ruby
ruby-on-rails ruby
asked Nov 15 at 1:49
Steve
103
103
What do you mean "fee which is an integer"? Do you mean your model has afee
field that is of the typeinteger
?
– jvillian
Nov 15 at 1:55
fee
is just the name of the column in the table, with datatype integer. Sorry if i'm not using the correct terminology - i'm relatively new to programming.
– Steve
Nov 15 at 2:00
If the data type is integer, then you can only store an integer in it. If you want to store something else, then you'll need to change the data type. If you're using it to store money-like values, then consider a Big Decimal. You can Google around for the pros and cons of using different data types to store money values.
– jvillian
Nov 15 at 2:07
what datatype should be used for money?
– Steve
Nov 15 at 2:09
1
Just to be clear: It is fully possible and quite common to store monetary values as integers in databases. The values are stored in the type of currency (e.g. cents), so storing3.05$
becomes305
in the database.
– Marcin Kołodziej
Nov 15 at 2:56
|
show 3 more comments
What do you mean "fee which is an integer"? Do you mean your model has afee
field that is of the typeinteger
?
– jvillian
Nov 15 at 1:55
fee
is just the name of the column in the table, with datatype integer. Sorry if i'm not using the correct terminology - i'm relatively new to programming.
– Steve
Nov 15 at 2:00
If the data type is integer, then you can only store an integer in it. If you want to store something else, then you'll need to change the data type. If you're using it to store money-like values, then consider a Big Decimal. You can Google around for the pros and cons of using different data types to store money values.
– jvillian
Nov 15 at 2:07
what datatype should be used for money?
– Steve
Nov 15 at 2:09
1
Just to be clear: It is fully possible and quite common to store monetary values as integers in databases. The values are stored in the type of currency (e.g. cents), so storing3.05$
becomes305
in the database.
– Marcin Kołodziej
Nov 15 at 2:56
What do you mean "fee which is an integer"? Do you mean your model has a
fee
field that is of the type integer
?– jvillian
Nov 15 at 1:55
What do you mean "fee which is an integer"? Do you mean your model has a
fee
field that is of the type integer
?– jvillian
Nov 15 at 1:55
fee
is just the name of the column in the table, with datatype integer. Sorry if i'm not using the correct terminology - i'm relatively new to programming.– Steve
Nov 15 at 2:00
fee
is just the name of the column in the table, with datatype integer. Sorry if i'm not using the correct terminology - i'm relatively new to programming.– Steve
Nov 15 at 2:00
If the data type is integer, then you can only store an integer in it. If you want to store something else, then you'll need to change the data type. If you're using it to store money-like values, then consider a Big Decimal. You can Google around for the pros and cons of using different data types to store money values.
– jvillian
Nov 15 at 2:07
If the data type is integer, then you can only store an integer in it. If you want to store something else, then you'll need to change the data type. If you're using it to store money-like values, then consider a Big Decimal. You can Google around for the pros and cons of using different data types to store money values.
– jvillian
Nov 15 at 2:07
what datatype should be used for money?
– Steve
Nov 15 at 2:09
what datatype should be used for money?
– Steve
Nov 15 at 2:09
1
1
Just to be clear: It is fully possible and quite common to store monetary values as integers in databases. The values are stored in the type of currency (e.g. cents), so storing
3.05$
becomes 305
in the database.– Marcin Kołodziej
Nov 15 at 2:56
Just to be clear: It is fully possible and quite common to store monetary values as integers in databases. The values are stored in the type of currency (e.g. cents), so storing
3.05$
becomes 305
in the database.– Marcin Kołodziej
Nov 15 at 2:56
|
show 3 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
It looks like your are storing your value in integer datatype you can change it in float to store and show values after decimal point write following migration and add following line to change the datatype in your migration.
rails g migration change_fee_to_be_float_in_orders
and in your migration
class ChangeFeeToBeFloatInOrders
def up
change_column :orders, :fee, :float
end
def down
change_column :orders, :fee, :integer
end
end
will needup
anddown
here to make migration reversible.
– Hussain Bhatti
Nov 15 at 5:03
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
It looks like your are storing your value in integer datatype you can change it in float to store and show values after decimal point write following migration and add following line to change the datatype in your migration.
rails g migration change_fee_to_be_float_in_orders
and in your migration
class ChangeFeeToBeFloatInOrders
def up
change_column :orders, :fee, :float
end
def down
change_column :orders, :fee, :integer
end
end
will needup
anddown
here to make migration reversible.
– Hussain Bhatti
Nov 15 at 5:03
add a comment |
up vote
0
down vote
It looks like your are storing your value in integer datatype you can change it in float to store and show values after decimal point write following migration and add following line to change the datatype in your migration.
rails g migration change_fee_to_be_float_in_orders
and in your migration
class ChangeFeeToBeFloatInOrders
def up
change_column :orders, :fee, :float
end
def down
change_column :orders, :fee, :integer
end
end
will needup
anddown
here to make migration reversible.
– Hussain Bhatti
Nov 15 at 5:03
add a comment |
up vote
0
down vote
up vote
0
down vote
It looks like your are storing your value in integer datatype you can change it in float to store and show values after decimal point write following migration and add following line to change the datatype in your migration.
rails g migration change_fee_to_be_float_in_orders
and in your migration
class ChangeFeeToBeFloatInOrders
def up
change_column :orders, :fee, :float
end
def down
change_column :orders, :fee, :integer
end
end
It looks like your are storing your value in integer datatype you can change it in float to store and show values after decimal point write following migration and add following line to change the datatype in your migration.
rails g migration change_fee_to_be_float_in_orders
and in your migration
class ChangeFeeToBeFloatInOrders
def up
change_column :orders, :fee, :float
end
def down
change_column :orders, :fee, :integer
end
end
answered Nov 15 at 5:01
Hussain Bhatti
363
363
will needup
anddown
here to make migration reversible.
– Hussain Bhatti
Nov 15 at 5:03
add a comment |
will needup
anddown
here to make migration reversible.
– Hussain Bhatti
Nov 15 at 5:03
will need
up
and down
here to make migration reversible.– Hussain Bhatti
Nov 15 at 5:03
will need
up
and down
here to make migration reversible.– Hussain Bhatti
Nov 15 at 5:03
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311309%2fruby-integer-always-rounds-to-the-nearest-whole-number-and-wont-save-decimal-pl%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you mean "fee which is an integer"? Do you mean your model has a
fee
field that is of the typeinteger
?– jvillian
Nov 15 at 1:55
fee
is just the name of the column in the table, with datatype integer. Sorry if i'm not using the correct terminology - i'm relatively new to programming.– Steve
Nov 15 at 2:00
If the data type is integer, then you can only store an integer in it. If you want to store something else, then you'll need to change the data type. If you're using it to store money-like values, then consider a Big Decimal. You can Google around for the pros and cons of using different data types to store money values.
– jvillian
Nov 15 at 2:07
what datatype should be used for money?
– Steve
Nov 15 at 2:09
1
Just to be clear: It is fully possible and quite common to store monetary values as integers in databases. The values are stored in the type of currency (e.g. cents), so storing
3.05$
becomes305
in the database.– Marcin Kołodziej
Nov 15 at 2:56