Ruby integer always rounds to the nearest whole number and won't save decimal places
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
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 '18 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.
– user10410465
Nov 15 '18 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 '18 at 2:07
what datatype should be used for money?
– user10410465
Nov 15 '18 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 '18 at 2:56
|
show 3 more comments
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 '18 at 1:49
user10410465
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 '18 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.
– user10410465
Nov 15 '18 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 '18 at 2:07
what datatype should be used for money?
– user10410465
Nov 15 '18 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 '18 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 '18 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.
– user10410465
Nov 15 '18 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 '18 at 2:07
what datatype should be used for money?
– user10410465
Nov 15 '18 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 '18 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 '18 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 '18 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.– user10410465
Nov 15 '18 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.– user10410465
Nov 15 '18 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 '18 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 '18 at 2:07
what datatype should be used for money?
– user10410465
Nov 15 '18 at 2:09
what datatype should be used for money?
– user10410465
Nov 15 '18 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 '18 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 '18 at 2:56
|
show 3 more comments
1 Answer
1
active
oldest
votes
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 '18 at 5:03
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 at 5:03
add a comment |
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 '18 at 5:03
add a comment |
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 '18 at 5:01
Hussain BhattiHussain Bhatti
363
363
will needup
anddown
here to make migration reversible.
– Hussain Bhatti
Nov 15 '18 at 5:03
add a comment |
will needup
anddown
here to make migration reversible.
– Hussain Bhatti
Nov 15 '18 at 5:03
will need
up
and down
here to make migration reversible.– Hussain Bhatti
Nov 15 '18 at 5:03
will need
up
and down
here to make migration reversible.– Hussain Bhatti
Nov 15 '18 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.
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 '18 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.– user10410465
Nov 15 '18 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 '18 at 2:07
what datatype should be used for money?
– user10410465
Nov 15 '18 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 '18 at 2:56