输入数字乘以字符串而不是数字 - Rails 4
Input number multiplies as a string instead of number - Rails 4
我希望你关注这条线conversion = value * 7.50061
考虑以下问题。当我打印初始值为 12 的转换时,我得到 7 乘以 12,就像这样 12121212121212
。可能那是因为我正在乘以一个字符串但是当我尝试
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
计算控制器
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
感谢任何帮助
conversion = value.to_f * 7.50061
你在正确的轨道上,你首先需要将字符串 value
转换为浮点数或整数来用它进行计算。如果字符串是 12
,则将其转换为整数可能更有意义:
conversion = value.to_i * 7.50061
您在下一行 ("this is the " + conversion
) 中收到 implicit conversion from float to string
错误,因为您尝试向字符串添加浮点数。
改为:
puts "this is the #{conversion}"
我希望你关注这条线conversion = value * 7.50061
考虑以下问题。当我打印初始值为 12 的转换时,我得到 7 乘以 12,就像这样 12121212121212
。可能那是因为我正在乘以一个字符串但是当我尝试
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
计算控制器
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
感谢任何帮助
conversion = value.to_f * 7.50061
你在正确的轨道上,你首先需要将字符串 value
转换为浮点数或整数来用它进行计算。如果字符串是 12
,则将其转换为整数可能更有意义:
conversion = value.to_i * 7.50061
您在下一行 ("this is the " + conversion
) 中收到 implicit conversion from float to string
错误,因为您尝试向字符串添加浮点数。
改为:
puts "this is the #{conversion}"