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










share|improve this question






















  • 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










  • 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$ becomes 305 in the database.
    – Marcin Kołodziej
    Nov 15 at 2:56















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










share|improve this question






















  • 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










  • 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$ becomes 305 in the database.
    – Marcin Kołodziej
    Nov 15 at 2:56













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










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 at 1:49









Steve

103




103












  • 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










  • 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$ becomes 305 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










  • 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$ becomes 305 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












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





share|improve this answer





















  • will need up and down here to make migration reversible.
    – Hussain Bhatti
    Nov 15 at 5:03











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',
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
});


}
});














draft saved

draft discarded


















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








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





share|improve this answer





















  • will need up and down here to make migration reversible.
    – Hussain Bhatti
    Nov 15 at 5:03















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





share|improve this answer





















  • will need up and down here to make migration reversible.
    – Hussain Bhatti
    Nov 15 at 5:03













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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 at 5:01









Hussain Bhatti

363




363












  • 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
















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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?