i)
string = "$575.00 "
string.to_f
// => 0.0
string = "575.00 "
string.to_f
// => 575.0
以这种形式产生的价值,需要将任何建议纳入一个数据库领域。
"$575.00 "
i)
string = "$575.00 "
string.to_f
// => 0.0
string = "575.00 "
string.to_f
// => 575.0
以这种形式产生的价值,需要将任何建议纳入一个数据库领域。
"$575.00 "
我们这样做时常写到“_cost_to_f
:
class String
def cost_to_f
self.delete( $, ).to_f
end
end
我们将这些延期储存在<代码>config/ originalizers/extensions/string.rb。
那么,你可以简单地说:
"$5,425.55".cost_to_f #=> 5425.55
如果你很少使用这种方法,最好的办法就是简单地建立一种功能,因为增加核心班的职能并不是我建议的:
def cost_to_f(string)
string.delete( $, ).to_f
end
如果你需要不止一个班级,你可以总是把它放在一个单元中,然后在需要时将这个模块放在<条码>。
还有一个比喻。 您提到,在准备提交数据库时,你需要处理这一缺陷。 积极开展工作的最佳途径是:
class Item < ActiveRecord::Base
def price=(p)
p = p.cost_to_f if p.is_a?(String)
write_attribute(:price, p)
end
end
www.un.org/Depts/DGACM/index_spanish.htm 更新后使用<条码> 缩略语
如此之多的答复...... i 试图总结现在可以提供的所有材料,然后自行回答。
1. string.gsub(/[$,]/, )
string.gsub!(/^$/, )
2. string[1..-1]
3. string.slice(0) # => "ome string"
4. s/^.//
(g)sub
and regexp Just for deleting a character? String#tr
is faster and shorter. String#delete
is even better.String#[]
And my advice is: What if you have not dollar, but yena? Or what if you don t even have anything before numbers? So i ll prefer:
string[/d.+/]
这将产生防止<条码>至_f条码>的良好工作的主要非决定性符号。
P.S.: By the way. It s known, that float
is bad practice for storing money amounts.
Use Float or Decimal for Accounting Application Dollar Amount?
你可以尝试这样的东西。
string = string[1..-1] if string.match(/^$/)
或者这样。
string.gsub!(/^$/, )
请大家把这种反弹放在你的雷克中,这也意味着“到底”。
您可为此使用reg:
s/^.//
作为通道,这是PCRE syntax。
在Ruby, 您可使用sub(
)方法,取代插图:
result = string.sub(/^./,"")
这应当行之有效。
<><>>
Ok询问gsub(
)对于:
<代码>gsub(, 诸如sub(>>
,但与/g
modifier in PCRE (关于全球替换):
s/a/b/
《刑法》
string.sub(/a/, "b")
以及
s/a/b/g
是
string.gsub(/a/, "b")
页: 1
我的用法(而不是常规表述)只是载于<代码>slice!。 <代码>String类别中的方法。 例如,
s = "Some string"
s.slice!(0) # Deletes and returns the 0th character from the string.
s # => "ome string"
collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...
We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...
As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...
I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?
I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...
I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml
guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...
I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...