Allowing/Replacing Comma as Number Separator for Input Type="Number" Using Rails

Add commas for a number in input field while typing

if you wanted to have something like "1 000 000", you can change the code to something like this.

  var num = $this.val().replace(/(\s)/g, '');
$this.val(num.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1 "));

Hope this helps

Rails: Is there a rails trick to adding commas to large numbers?

You want the number_with_delimiter method. For example:

<%= number_with_delimiter(@number, :delimiter => ',') %>

Alternatively, you can use the number_with_precision method to ensure that the number is always displayed with two decimal places of precision:

<%= number_with_precision(@number, :precision => 2, :delimiter => ',') %>

Input mask for numeric and decimal

You can use jquery numeric for numbers.

The current version does allow what you're looking for but someone has changed the code a little bit and it works:


<input class="numeric" type="text" />


$(".numeric").numeric({ decimal : ".",  negative : false, scale: 3 });

This is the whole source.

And I've prepared this fiddle so you can see how it works.

Convert comma to point as delimiter

What you're doing may not be the best way, so perhaps someone can answer with a better approach. But to get your line working you need to make it actually persist the change.

self.price.to_s.gsub(',', '.').to_f

Will just return the change, but that doesn't go anywhere in a callback!

self.price = self.price.to_s.gsub(',', '.').to_f
# OR
self.price.to_s.gsub!(',', '.').to_f

Will persist the change within the object.

Related Topics

Leave a reply