输入数字乘以字符串而不是数字 - Rails 4

时间:2015-11-22 15:27:38

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

我希望您专注于conversion = value * 7.50061

请考虑以下事项。当我打印初始值为12的转换时,我会像这样12121212121212得到7次12。可能那是因为我正在乘以一个字符串,但当我尝试

value.to_i我发送错误implicit conversion from float to string??

我从视图中的输入元素提交一个值,然后将其从CalculationsController输入到CalculationsHelper

module CalculationsHelper

  # parameter1 = Sa02, parameter2 = Hgb, parameter3  = PaO2
  def AOC(parameter1,parameter2,parameter3,unit_parameter2,unit_parameter3)
    # CaO2 = ( Hgb * 1.34 * SaO2 / 100 ) + ( PaO2 * 0.031 )
    if (unit_parameter3 != "mmHg")
      puts "entered conversions"
      conversions(unit_parameter3,parameter3, "mmHg")
    end
  end

  def conversions(input, value, target)
    if (target == "mmHg")
      if (input == "Kpa")
        puts "this is the value before " + value
        conversion = value * 7.50061
        puts "this is the " + conversion
      end
    end
  end

end

CalculationsController

class CalculationsController < ApplicationController
  include CalculationsHelper

  def index

  end

  def calculation

    if (params["equation"] == "AOC")
      puts "entered AOC"
      AOC(params["parameter1"],params["parameter2"],params["parameter3"],params["unit_parameter2"],params["unit_parameter3"])
    end
    respond_to do |format|
      format.json {
        render json: { success: "ok" }
      }
    end
  end
end

任何帮助表示赞赏

2 个答案:

答案 0 :(得分:1)

你走在正确的轨道上,首先需要将字符串value转换为浮点数或整数来进行计算。如果String是12,将它转换为整数可能更有意义:

conversion = value.to_i * 7.50061

您在下一行(implicit conversion from float to string)中收到"this is the " + conversion错误,因为您尝试向字符串添加浮点数。

改为写:

puts "this is the #{conversion}"

答案 1 :(得分:0)

.EntireRow.Delete