2014-10-16 57 views
0

我需要显示在视图中的结果
但格式不漂亮
我的代码Rails的显示结果,鉴于格式

 - @patient.weight.each do |m| 
    tr 
     td = m.date 
     td = m.weight 
     td = m.weight/(@patient.weight.first.inch * @patient.weight.first.inch).to_f 

这一结果在网页

02/09/2014 23.44 6.4930747922437675 

我需要证明只在第3栏6.49
需要帮助

回答

0

你可以使用round,并使用包含所需小数的参数。

>> 6.4930747922437675.round(2) 
=> 6.49 

所以,只要改变你的行

td = (m.weight/(@patient.weight.first.inch * @patient.weight.first.inch).to_f).round(2) 

这解决我的问题,以避免在视图误差修改

- @patient.weight.each do |m| 
    tr 
     - if m.date != nil 
      td = m.date 
     - else 
      td = 'Sem pesos registrados' 

     - if m.weight != nil 
      td = m.weight 
     - else 
      td = 'sem peso registrado' 

     - if @patient.weight.first.inch != nil 
      td = (m.weight/(@patient.weight.first.inch**2).to_f).round(2) 
     - else 
      td = 'sem dados para este calculo' 

BR

+0

我不知道正确地理解你的意思。如果我是对的,你可以做'''td =(@ patient.weight> 0?(m.weight /(@ patient.weight.first.inch * @ patient.weight.first.inch).to_f) “(2):”对于病人没有重量“)'''' – 2014-10-17 12:11:17

0

缩写形式将

td = (m.weight/(@patient.weight.first.inch**2).to_f).round(2) 

如果你总是希望有精度为2,你也可以使用number_with_precision助手(Docs

td = number_with_precision(m.weight/(@patient.weight.first.inch**2).to_f, precision: 2) 
+0

如果病人没有体重显示病人没有体重,我该如何放一个代码? – 2014-10-17 11:26:43