Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将BigDecimal ActiveRecord转换为Ruby字符串_Ruby - Fatal编程技术网

将BigDecimal ActiveRecord转换为Ruby字符串

将BigDecimal ActiveRecord转换为Ruby字符串,ruby,Ruby,我从数据库中得到如下记录: @foo = Foo.find(params[:foo_id]) @val = @foo.value.to_str # ERROR here 我需要整个@foo记录,但我需要使用ActiveRecord中的值字段(类型为BigDecimal)。我想我能做到这一点(?): 但是如何将@val转换为字符串?我找到了一个to_str方法,但我得到了: undefined method `to_str' for #<BigDecimal:7f88d4e5fa10

我从数据库中得到如下记录:

@foo = Foo.find(params[:foo_id])
 @val = @foo.value.to_str # ERROR here
我需要整个
@foo
记录,但我需要使用ActiveRecord中的
字段(类型为BigDecimal)。我想我能做到这一点(?):

但是如何将
@val
转换为字符串?我找到了一个
to_str
方法,但我得到了:

 undefined method `to_str' for #<BigDecimal:7f88d4e5fa10,'0.5155170589 999999E2',27(45)>

您需要使用
to_s
方法,这是在Ruby中将任何对象转换为字符串的标准方法。

to_str
用于隐式强制转换;BigDecimal不支持对字符串进行隐式转换<代码>#to_s是用于显式强制类型转换,正如Michael指出的。再次感谢Michael。但是,这是原始编号:
51.5517058999999
然后在使用
to_s
后,我得到
NTEuNTUxNzA1ODk5OTk5OTk=
我写的你应该使用它来编辑评论(对不起)。。我确实使用了
到irb(main):001:0>(51.5517058999999)。到“51.5517058999999”这是不可能的。您添加的结果是使用base64编码的此值
 @val = @foo.value.to_str # ERROR here