简体   繁体   English

将String转换为BigDecimal红宝石

[英]Convert String to BigDecimal ruby

I am making a rails site and I am trying to take a text box input and convert it to a big decimal value to pass to a backend service however when I take the input (params[:amount]) and do what I think should convert this to a BigDecimal I get an error when attempting to call the service saying "error cannot convert String to BigDecimal" on the line of the service call. 我正在制作一个Rails站点,并且尝试获取文本框输入并将其转换为大十进制值以传递给后端服务,但是当我获取输入(params [:amount])并执行我认为应该转换的操作时这到BigDecimal尝试调用该服务时,在服务调用行上显示“错误无法将String转换为BigDecimal”时出现错误。 See approximate code below 请参阅下面的近似代码

@amt = BigDecimal(params[:amount])

Service.call(@amt)

The error message indicates it is expecting a string in Service.call and wants to do the conversion itself. 错误消息表明它在Service.call中需要一个字符串,并且想自己进行转换。 Assuming you have reasons for creating @amt (such as validation/error checking), I would pass in @amt.to_s and see what happens 假设您有创建@amt的理由(例如验证/错误检查),我将传入@ amt.to_s并查看会发生什么

Barring that, we need the code behind Service.call() to know more. 除此以外,我们需要Service.call()背后的代码才能了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM