I am trying give decimal value to input field like - 10,235,333. It takes this value but as I move to next field, commas are removed from text field. Is there any way that commas will be left in the text field until I submit the form but when I submit the form only numbers will be stored in the database?
Thanks in advance..
I have got the solution for this issue. I got a jquery plugin for this that is jquery.number. This plugin automatically adds comma to your numbers.
This plug-in formats input value implicitly.
like if we type
10000 it formats this no to 10,000
for 1000 = 1,000
for 100000 = 100,000
for 1000000 = 1,000,000
you can override the setter method. Given a price field,
# model
def price=(num)
write_attribute :price, price.gsub(/[^\d]/, '')
end
Then in your view, you can format the value before you render the view
f.text_field :price, value: number_with_delimiter(f.object.price)
Related
In my project I am working with activemerchant gem where when I give credit card number it is stored as "xxxx-xxx-xxx-1111". Here there is an issue. While we try to submit / input the details, in the log my credit card number is displayed (ex: 1111-1111-1212-1111) as plain text but while storing in database it is fine.
While entering itself I want to see number in the log as "xxxx-xxxx-xxxx-1111". How it can be possible? Please help.
For this the best would be to use config.filter option (config/application.rb). For example password is automatically filtered. You can filter cc number so it appears as FILTERED, but if you want to add custom where you show last 4 when you can use labda operator in config.filter statement
config.filter_parameters << lambda do |k, v|
if k == 'cc_number'
v.replace('[FILTER]') #here you cann add your logic to extract last 4
end
end
For me I do not see any value in adding last 4, so simple filter would do on this field.
hope it helps
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
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?
I am facing an issue with Ruby BigDecimal on my Rails 4 app.
I have a "price" attribute in a Thing model, which class is BigDecimal. Whenever I create or update a Thing, I would like to save the "price" attribute with strictly two decimal digits (e.g. "0.00", "10.00", "10.10", "10.01").
But it saves, by default, only one decimal digit if two are not necessary. Here what I insert and what it saved currently :
"0" => "0.0" (instead of "0.00")
"10" => "10.0" (instead of "10.00")
"10.1" => "10.1" (instead of "10.10")
"10.01" => "10.01" (OK)
I don't just want to display the "price" attribute with two decimals (which can be done using "%.2f" % price), I want to save the "price" attribute with two decimal digits in my database. Any way to do so ?
Thanks a lot for your help.
What you are describing seems to me to be purely for display purposes. I don't know of any way to save a decimal to a DB with a specific amount of decimals that don't actually add precision unless you save them as a string.
Seems to me you would be best off creating a method on the Thing model that will return price formatted how you want.
on Delphi XE2,
I have a ClientDataSet which have many fields as Name, ...
It have a field named Date, as value type String. Containing a Date (dd/mm/yyyy)
I want to print content of ClientDataSet, using FastReport.
I want before to sort content ascending according to the Date field. I'm using index.
But when doing this, sorting does only sorts fields according to the content of the Date string before the "/".
form example dates like : 12/11/2012, 15/10/2012, 01/12/2012 are sorting like this : 01/12/2012 - 12/11/2012 - 15/10/2012.
ny idea how doing this correctly ?!
The sorting is correct! As you have a string field, the sorting is made like strings are sorted i.e. from left to right. If you want it sorted by Date you need either a date field or sort the string representation like yyyy/mm/dd.
You have some options:
Bring the field as a DateTime field. You would have to change the original SQL to that.
Do what Marjan suggested, bringing that string field formatted on an ISO-like style (which allows for the field to be ordered cronologically when string sorting is aplied) and creating an calculated field for user-display formatting.
Creating a new field on the TDatasetProvider's OnGetRecords event and populating it as a Date field.
Similar as above but creating a string field with the date formatted in ISO-Like style.
I personally suggest the first approach if possible.
I want to manipulate float numbers in gsp, here is what i want:
If the number has a 1.* i want it to show the dot, but if it ends with zero i dont want it to show the dot and zero.
like this:
Score: 1.5
Score: 1
Score: 2.1
Score: 3
The score variable is a float number and it is an input field on the gsp that loads the number and it can be changed.
But the real problem is, how can i see if the number has decimals?
There is already a taglib for formating numbers: (g:formatNumber)
I think something like this should work:
<g:formatNumber number="${score}" type="number" format="###.##"/>
But...if that doesn't work...
I would say write your own custom taglib. If it is something that is going to be used multiple times, why loop through a list of objects in your controller, change the float to a string just to display it? Let the page decide how to show it in the proper context.
Or
Add a transient field to the domain object (String scoreDisplay) and then have getScoreDisplay() return the value of score as a string, formatted how you want.
Well, I'd suggest you to format the number in your controller - before it gets to your gsp. That way you have more control over the number format. Once you're in the gsp, you have to use a grails' decimal number format or make your own taglib to format (since the number of fractional digits changes in your case).