- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
true do |t| t.decimal "cost", :precision => 10, :scale -6ren">
假设这样一个模式:
create_table "bills", :force => true do |t|
t.decimal "cost", :precision => 10, :scale => 5
end
我想编写一个函数,将新账单写入数据库,前提是它是唯一的。以下不起作用:
def load_bill_unless_exists(candidate)
incumbents = Bill.scoped.where(:cost => candidate.cost)
candidate.save unless incumbents.exists?
end
因为现任法案和候选人法案在他们的 BigDecimal 表示中有不同的限制,所以 :cost => candidate.cost
测试失败。也就是说,它正在比较:
candidate: #<Bill id: nil, cost: #<BigDecimal:105e39850,'0.1670576666 6666666E4',27(27)>>
与
incumbent: #<ServiceBill id: 198449, cost: #<BigDecimal:105e35840,'0.167057667E4',18(18)>>
请注意,候选人的 BigDecimal 代表的成本比现任的多。
所以问题很简单:执行此比较的正确方法是什么?我考虑过 :cost => BigDecimal.new(candidate.cost.to_s, 18)
,但这感觉不对——例如,数字 18 从何而来?
最佳答案
尝试使用 BigDecimal#round
:
def load_bill_unless_exists(candidate)
incumbents = Bill.scoped.where(:cost => candidate.cost.round(5))
candidate.save unless incumbents.exists?
end
来自文档:
Round to the nearest 1 (by default), returning the result as a BigDecimal. If n is specified and positive, the fractional part of the result has no more than that many digits.
鉴于您在架构中指定了 5 的精度,这就是您在进行比较时应该四舍五入的精度。
关于ruby-on-rails - 如何比较 BigDecimal 与 ActiveRecord 小数字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6297230/
当你做这样的事情 BigDecimal bigDecimal = BigDecimal.ONE; 为什么bigDecimal成为一个新对象? 最佳答案 在这种情况下,变量(或字段)bigDecimal
我正在使用 BigDecimal 计算一些大实数。虽然我尝试了两种方法:BigDecimal.toString() 或 BigDecimal.stripTrailingZeros().toString
我广泛使用 BigDecimals。我经常需要进行长时间的计算,然后比较结果。由于这些漫长的计算,答案不是 1而是0.9999999... 。这就是为什么我无法精确比较 BigDecimals,而只能
我有一段代码有两个 BigDecimal 变量。这两个变量都被发送到方法格式(工作正常)然后打印。变量“d”被发送到格式方法,然后直接打印而不分配它(因为格式方法返回一个字符串)。问题出现在变量 a
我遇到过 java.math.BigDecimal 和 android.icu.math.BigDecimal 因为我需要在项目中使用 BigDecimal . 我意识到 Android BigDec
我在整个应用程序中使用 BigDecimal 来处理金钱 和百分比。但是,我需要一种方法来区分两者之间的用法(为了呈现它们,即在 JTable 中)。因此,我最初的想法是编写两个行为与 BigDeci
这应该很简单,但它正在爆炸。有什么想法吗? d = BigDecimal.new("2.0") YAML::load({:a => d}.to_yaml) TypeError: BigDecimal
我有两个 BigDecimal,我想根据它们的 significant digits 确定它们是否接近。 . 例如,请考虑以下内容: BigDecimal million = new BigDecim
我需要一种方法将其表达为有效的java代码:我有2个BigDecimals,我想知道较小的BigDecimal是否可以(当添加到较大的BigDecimals时) BigDecimal 一次)更改较大
我正在争论是使用 BigDecimal 和 BigInteger 还是仅使用 BigDecimal 来让我的生活更轻松,减少来回转换。在资源方面仅使用 BigDecimal 有缺点吗? 仅使用原始数据
我有以下代码: Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_45). scala> v
我有一个 java 客户端,它通过 GRPC 调用 Clojure 服务。问题是,当我们从他们的服务收到响应时,它会在大十进制的末尾添加字母。当我们尝试在 java 中将其转换为大十进制时,我们会收到
我仍在学习 Java,并且一直在阅读多个站点上的文章。我在 Java Code Geeks 找到了一篇文章我有一个问题。该文章正在解释开放/封闭原则。本文以对公司产品应用折扣的场景为例。第一部分代码如
我正在尝试对 List 中的多个 BigDecimals 求和.目前,我正在使用两个流,但如果可能的话,我希望只有一个流。我不确定如何以高效的方式重写下面的内容。 BigDecimal totalCh
下面两行代码有什么区别? BigDecimal one = new BigDecimal("1"); BigDecimal two = BigDecimal.ONE; 两条线是否相同? 谢谢! 最佳答
这个问题在这里已经有了答案: BigDecimal from Double incorrect value? (4 个答案) Convert double to BigDecimal and set
在 Java 中,来自另一个 bigDecimal.toString() 的新 BigDecimal 是否总是等于?例如 BigDecimal a = new BigDecimal("1.23
对于新变量的比较或初始化,您使用其中的哪一个会有所不同吗? 我知道 BigDecimal.ZERO 是 1.5 的功能,所以这是一个问题,但假设我使用的是 1.5,这有关系吗? 谢谢。 最佳答案 Bi
情况: public static double pi(int a) { return (BigDecimal.valueOf(53360*sqrt(640320))).divide(co
我正在尝试创建一个可以在 diesel 中用于插入的结构。具体来说,我正在使结构可插入。编译时出现此错误。 我有一个结构,我正试图通过 derive 属性使 Insertable 成为可能。我有一个名
我是一名优秀的程序员,十分优秀!