如何用数字求和完整的字符串? [英] How can sum complete string with number?

查看:34
本文介绍了如何用数字求和完整的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前如何保存数字

这是我的桌子:

|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

这是控制器:

def new
  @customer = Customer.find(params[:id])
  @num= Product.first
  @other_value = Customer.count(:conditions=>['state=0'])
end

这是我的观点:

<% (@num.num_product)+@other_value %>

但它返回 00403 而不是 0043.我试过了,但我得到 403 没有 00:

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?

推荐答案

你为什么不这样做:

"%04d" % (@num.num_product.to_i + 1)

给定 @num.num_product #=>0040" 然后:

"%04d" % ('0040'.to_i + 1)
=> "0041"

这篇关于如何用数字求和完整的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆