简体   繁体   中英

bug when i input value “45” i get “0.45e2” ruby on rails

i cant understand why when i input 45 i get 0.45e2 while any sting like pants work fine i have the following html

<%= form_with(model: invoice, local: true) do |form| %>
  <% if invoice.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(invoice.errors.count, "error") %> prohibited this invoice from being saved:</h2>
      <ul>
        <% invoice.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
<center><h1>ΤΔΑ-ΤΙΜΟΛΟΓΙΟ-ΔΕΛΤΙΟΥ ΑΠΟΣΤΟΛΗΣ</h1></center>
<div align="right">

</div>

<pre>ID           CODE         DESCRIPTION  BASE CODE    QUANTITY     </pre>
<table style="border:2px solid black;"id="tableid">
<td><input  type="text" name="invoice[items][]" /></td>
<td><input  type="number" name="invoice[tax][]" /></td>
<td><input  type="number" name="invoice[discount][]" /></td>
<td><input  type="number" name="invoice[sum][]" /></td>
<td><input  type="number" name="invoice[price][]" /></td>
</tr>
</table>
<script type="text/javascript">
    var table = document.getElementById("tableid");
    table.addEventListener("keyup", function(event) 
    {
    if (event.keyCode === 18)
     {
            event.preventDefault();
            var rowid=table.rows.length-1;
            var row = table.insertRow(rowid+1);
            row.id = (rowid+1).toString();
            var cell0 = row.insertCell(0);
            var cell1 = row.insertCell(1);
            var cell2 = row.insertCell(2);
            var cell3 = row.insertCell(3);
            var cell4 = row.insertCell(4);
            cell0.innerHTML = "<input type='text'  size='8' name='invoice[items][]'></input>";
        cell1.innerHTML = "<input type='text'  size='8' name='invoice[tax][]'></input>";
        cell2.innerHTML = "<input type='text'  size='8' name='invoice[discount][]'></input>";
        cell3.innerHTML = "<input type='text'  size='8' name='invoice[sum][]'></input>";
        cell4.innerHTML = "<input type='text'  size='8' name='invoice[price][]'></input>";

      }
    });
</script>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

and my controller

    def invoice_params
      params.require(:invoice).permit({:items => []}, {:price => []}, {:tax => []}, {:discount => []}, {:sum => []})
    end

so this is happening when i create new items

Invoice was successfully created.

Items: ["pants", "spoon"]

Price: [0.45e2, 0.2e1]

Tax: [0.45e2, 0.25e2]

Discount: [0.45e2, 0.2e2]

Sum: [0.45e2, 0.22e2]

Edit | Back

should i tell ruby on rails tat the params sum,tax,discoun is numeric or something like that? or must be a better way to save the params? as the string work fine

UPDATE also if you change t.numeric to t.float solve the problem

It's best to avoid floating point numbers completely when dealing with money and convert to eg cents (or whatever your currency's smallest denomination is called) for storage. If you know you'll only ever need to support one currency this will be simple. Of course, you still want to allow for entering and displaying prices via decimal notation. Here's how I would approach that in your Invoice model, after creating new price_cents etc. columns and migrating the existing data:

  def price=(value)
    value = value.to_f if value.is_a? String
    self.price_cents = (value * 100).round
  end

  def price
    price_cents&.positive? ? price_cents / 100.0 : 0
  end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM