Dan
Dan

Reputation: 381

Irregularities handling and showing decimal values in forms

i have an issue for which i have not been able to find a specific solution for.

I have a Ruby on Rails Webapp that handles data entry of specific invoice amounts. Simply a form where a user can punch information from an invoice. The values are rather simple. id, Year, Month and then a bunch of amount fields.

When i first created the amount columns in the postgreSQL database, i didn't specify any precision and i didn't default the values to 0. I found out quickly that if i didn't want to assign 0 values to any nil returned from the form, it was better to assign a default value right away.

The issue i have is that when a new form (or the edit form) is rendered, in some browsers (not all) any 0.00 value shows as 0 or 0.0 even though my columns have a precision of 2 and i am formatting the value of the form field using :value => (number_with_precision(f.object.consulting, :precision => 2) || 0)

To make the matter worse, the same browser reacts differently on different platforms. Mozilla Firefox on my Ubuntu machine shows the values as 0.00 whereas FireFox on my windows machine shows the values as 0

When you do type a decimal such as 100.25, then both decimals are showing. If i were to type 100.20, only 100.2 would be shown.

Here is what i have:

In my table, columns are identified as such:

t.decimal  "consulting",       precision: 10, scale: 2, default: 0.0

In my view/form, i use the following code to display the column

<div class = "col-sm-5 col-md-5 col-lg-3">
      <%=f.number_field :consulting,  :step => 'any', :tabindex => 5 ,class: 'form-control text-right input-sm remit-sum', :value => (number_with_precision(f.object.consulting, :precision => 2) || 0.00) %>
</div>

I am not quite sure what i could do to fix the issue. I know that if i change the form field from

f.number_field to f.text_field

It works and displays properly. So i guess this is a 2 part question. 1)Ss there a way to consistently display 2 decimals using the number_field tag 2) What harm would it do and what kind of changes would it take to display and input the amouts using a text_field tag.

ps: I have tried using the number_to_currency method to no avail

Upvotes: 0

Views: 321

Answers (1)

max
max

Reputation: 102240

So there a way to consistently display 2 decimals using the number_field tag?

You can set step="0.01" unfortunately that also sets the step so that incrementing/decrementing with the buttons would only change the amount by 1¢ for euros or dollars. Which means you would have to tap it one hundred times on a smartphone to change the amount by one unit which is a fail.

Most browsers drop the trailing zeros as (0.00 = 0.0 = 0). You might just want to learn to live with basic mathematics.

What harm would it do and what kind of changes would it take to display and input the amounts using a text_field tag.

Back in the day before HTML5 thats how it was done. You lose the native form controls and need to format the values with javascript - remember those clunky jQuery UI number selects?

Upvotes: 0

Related Questions