Reputation: 1149
How can I save numbers before
Here is my table:
|products|
int varchar(10)
|id| |num_product|
1 0040
|customers|
int varchar(255)
|id| |name| |state|
1 ABC 0
2 DEF 0
3 GHI 0
4 JKL 1
Here is the controller:
def new
@customer = Customer.find(params[:id])
@num= Product.first
@other_value = Customer.count(:conditions=>['state=0'])
end
Here is my view:
<% (@num.num_product)+@other_value %>
But it returns 00403
instead of 0043
. I tried this but I'm getting 403
without 00
:
<% (@num.num_product.to_i)+@other_value.to_i %>
Also I tried this but is not the correct way:
<% "000"+(@num.num_product.to_i)+@other_value.to_i %>
Can somebody help me about this?
Upvotes: 2
Views: 714
Reputation: 7735
Why dont you do:
"%04d" % (@num.num_product.to_i + 1)
Given @num.num_product #=> "0040"
then:
"%04d" % ('0040'.to_i + 1)
=> "0041"
Upvotes: 4