This question already has answers here:
How to strip commas from float input?
(3 answers)
Closed 8 years ago.
I searched for a while for a ruby or rails method that I can call to convert a simple string like 1,000.53 into a float value but I couldn't.
All I could see is number_to_human which does the reverse of what I want. Is there anything available for my use case or am I alone (which I dont think). Also all I want is a simple conversion as above with commas and dot. No fancy currency or other any other notation conversion needed.
Thanks
EDIT
'1,000.5'.gsub(/,/, '').to_f works but I am looking for an already available method in ruby or rails. Or a better alternative to my solution with gsub.
First, remove all chars from the string that are not a digit or the separator (. in your example). Then call to_f on the sanitized string:
'1,000.53'.gsub(/[^\d.]/, '').to_f #=> 1000.53
to_f might work
Try "your_string".to_f
Related
This question already has answers here:
Trouble with floats in Objective-C
(3 answers)
Closed 7 years ago.
I am getting an unusal issue with float in Objective C. I enter 100.1 and i get 100.100002 shouldn't it be something like 100.100000 .
Following is the code
float temp=100.1;
NSLog(#"%f",temp);
100.100000
Can someone guide me what am i doing wrong or how to fix it ? I cannot use fixed decimal places i-e i cannot just use 100.10 . I need all decimal places .
Because that is a fundamental part of what happens when you represent an arbitrary floating point value in binary. The number of binary digits is limited, therefore rounding occurs. Depending on your needs, you might be better off using NSDecimalNumber.
Try using double instead;
double temp=100.1;
NSLog(#"%.8f",temp);
100.10000000
It is an issue with representation accuracy. I do not think it will be a problem to use double instead.
This question already has answers here:
What is <<- in ruby?
(2 answers)
Closed 7 years ago.
This bit of code is taken from Ryan Bates' Railscast episode 343 on full-text search in PostgreSQL. I would like to understand it, but I can't find anything on the <<- operator (if it even is an operator). Can someone please point me to somewhere I can learn about this?
rank = <<-RANK
ts_rank(to_tsvector(name), plainto_tsquery(#{sanitize(query)})) +
ts_rank(to_tsvector(content), plainto_tsquery(#{sanitize(query)}))
RANK
It is a multiline String in ruby, the contents are interpolated and then executed in PostgreSQL. This is a standard way to run scripts on the command line. I use it to write AWS Scripts from within Capistrano.
It uses the here-doc syntax.
http://blog.jayfields.com/2006/12/ruby-multiline-strings-here-doc-or.html
http://ruby-doc.org/core-2.2.0/doc/syntax/literals_rdoc.html#label-Here+Documents
It's official name is a heredoc and there are two different ways you can use them.
One is how you have it laid out where the start will be <<-NAME and the end will simple be NAME.
The other way you can do it is <<NAME but when closing you have to make sure there are no spaces before NAME on the line. Some example code below to show the difference.
def sample_method
heredoc1 = <<-NAME
This is a sample heredoc
NAME
heredoc2 = <<OTHERHEREDOC
Both of these are the same thing
OTHERHEREDOC
end
Both of these will work as heredocs, but as you can see the second one looks a little uglier. Choose whichever you prefer when using them yourself, but make sure to pay attention to white space and the end of string delimiter if you don't include the dash.
This question already has answers here:
Why is division in Ruby returning an integer instead of decimal value?
(7 answers)
Closed 8 years ago.
Should be Simple.
I'm using Rails and doing something along the lines of:
<% n = 15 / 2 %>
<%= n %>
However, whenever I output variable n, it doesn't give me a decimal.
Based on other calculations it looks like it's constantly rounding to floor, But I haven't added any sort of round method anywhere.
Could you explain why? I want it to actually round to ceil.
You can use Numeric#ceil
n = 15 / 2.0
n = n.ceil
Now, you need to either use .to_f or specify the numerator or denominator as a float ( 2.0 or 15.0) in order for it to be considered a floating point expression (as against an integer expression)
You asked for explanation as well - the explanation is that because there are no decimal points, ruby is treating your numbers as integers. When integer division is done in ruby, anything after the decimal point is truncated. In order to not lose that info, you need to tell ruby to do floating point arithmetic, which you can do in either of the ways Karthikr explained.
The issue is that 15 and 2 are not Floats but are Integers. You are doing Integer math.
You can coerce either to a float by using .to_f or adding that .0 and you will not have the same issue. If you want it to round up, then you can of course use .ceil
So, it isn't 'rounding to floor' it is a different concept.
I have a field -
:Revenue
and it should accept values like 10,000.00, but if I input such value it stores 10 into database instead of 10000.00
What should I do to strip of commas before I save?
I've tried to find a few solutions online but wasn't able to implement them as I found them incomplete. If someone could help me I would really appreciate it.
**The problem now I am facing is that as soon as I enter the value rails converts string in to float value before it can run the gsub function, like if I enter 50,000.00 its converting into float 50.0 before calling the gsub, is there any way to over the to_f method which rails is calling on the string.
Removing commas is pretty simple:
value.gsub(/,/, '').to_f
Keep in mind that European formatting often uses comma as the decimal value separator so your results would be off by a factor of 100 if processing those sorts of numbers.
You can take a String#delete.
"10,000,000.00".delete(',').to_f
# => 10000000.0
I found the solution after looking at few places and combining few solutions, since I had to use gsub before the linking to model has to be done. so I created the method in my controller and called it before create and update action. and wrote the following code in the method
params[:record][:Revenue] = params[:record][:Revenue].gsub(/,/,"")
I am trying to capture a conditional of years in RegEx. Basically, if they implement just a two digit year, I want to make it a four digit year. So if they put :
1/2/08
I want to make it :
1/2/2008
Any ideas?
One [pretty nasty] way using regex:
"1/2/08".sub! /\/(\d{2})$/, '/20\1'
Wouldn't it be better to just parse the string into a date object, though? Then you can treat it as a date properly! :)
You could split on '/' and if the last component has a length of two you prepend 20 and then assemble the date again.
You could split the string up using
(.*/)(..)$
and then substitute with something like
$120$2
to put the string back together (tested with http://www.regexplanet.com/simple/).
You might need to think about what you want to happen for dates in the 20th century - this approach will recognise 21/01/98 as 21/01/2098 which might not be what you want... It might be better to parse the string out properly rather than just blindly regex it!