bigdecimal

Ruby on Rails nil can't be coerced into BigDecimal

醉酒当歌 提交于 2019-11-29 07:16:46
Why do I get nil can't be coerced into BigDecimal when I try to perform a calculation: here's the code: model/drink.rb class Drink < ActiveRecord::Base belongs_to :menu before_save :total_amount def total_amount self.total_amount = self.price * self.quantity end model/menu.rb class Menu < ActiveRecord::Base has_many :drinks, :dependent => :destroy accepts_nested_attributes_for :drinks, :allow_destroy => true #Validations end * Drink is the (nested)child model and Menu the parent model When I attempt to create a new drink the browser display following error message nil can't be coerced into

generating pi to nth digit java

半腔热情 提交于 2019-11-29 06:55:21
I wanted to know how I can generate pi to the nth digit. I have a couple of basic ideas. Use Math.PI and increase the precision (if that's possible) Use Euler's formula to generate pi but even here, I would need to increase the precision (I think) There is also Srinivasa Ramanujan's formula for generating PI which is known for it's rapid convergence. This formula seems difficult to implement. I believe, I would have to also increase deicmal precision here. So in short, either way, I would need to increase the precision of BigDecimal depending on what the nth digit is. How would I go about

Java library to check whether a String contains a number *without* exceptions

走远了吗. 提交于 2019-11-29 06:48:54
I'm looking for a method that returns a boolean if the String it is passed is a valid number (e.g. "123.55e-9", "-333,556"). I don't want to just do: public boolean isANumber(String s) { try { BigDecimal a = new BigDecimal(s); return true; } catch (NumberFormatException e) { return false; } } Clearly, the function should use a state machine (DFA) to parse the string to make sure invalid examples don't fool it (e.g. "-21,22.22.2", "33-2"). Do you know if any such library exists? I don't really want to write it myself as it's such an obvious problem that I'm sure I'd be re-inventing the wheel.

Java Map.merge() 使用

旧时模样 提交于 2019-11-29 06:43:16
关于Map.merge()这可能是Key-Value中最通用的操作。但它也相当模糊很少使用。 merge() :它将新值置于给定键下(如果不存在)或更新具有给定值的现有键(UPSERT)。 示例 : //计算唯一的单词出现次数 var map = new HashMap<String, Integer>(); words.forEach(word -> { var prev = map.get(word); if (prev == null) { map.put(word, 1); } else { map.put(word, prev + 1); } }); //给定输入 对应的输出结果 var words = List.of("Foo", "Bar", "Foo", "Buzz", "Foo", "Buzz", "Fizz", "Fizz"); //... {Bar=1, Fizz=2, Foo=3, Buzz=2} //进行重构以避免条件逻辑 words.forEach(word -> { map.putIfAbsent(word, 0); map.put(word, map.get(word) + 1); }); putIfAbsent() 是必要的否则代码会在第一次出现未知的单词时中断。另外map.get(word) 里面map.put() 有点尴尬。优化 words

java取整和java四舍五入方法 BigDecimal.setScale()方法详解.

泪湿孤枕 提交于 2019-11-29 05:45:42
内容: import java.math.BigDecimal; import java.text.DecimalFormat; public class TestGetInt{ public static void main(String[] args){ double i=2, j=2.1, k=2.5, m=2.9; System.out.println("舍掉小数取整:Math.floor(2)=" + (int)Math.floor(i)); System.out.println("舍掉小数取整:Math.floor(2.1)=" + (int)Math.floor(j)); System.out.println("舍掉小数取整:Math.floor(2.5)=" + (int)Math.floor(k)); System.out.println("舍掉小数取整:Math.floor(2.9)=" + (int)Math.floor(m)); /* 这段被注释的代码不能正确的实现四舍五入取整 System.out.println("四舍五入取整:Math.rint(2)=" + (int)Math.rint(i)); System.out.println("四舍五入取整:Math.rint(2.1)=" + (int)Math.rint(j)); System.out

How to multiply a BigDecimal by an integer in Java

送分小仙女□ 提交于 2019-11-29 04:30:57
问题 How do you multiply a BigDecimal by an integer in Java? I tried this but its not correct. import java.math.BigDecimal; import java.math.MathContext; public class Payment { int itemCost; int totalCost = 0; public BigDecimal calculateCost(int itemQuantity,BigDecimal itemPrice){ itemCost = itemPrice.multiply(itemQuantity); totalCost = totalCost + itemCost; return totalCost; } 回答1: You have a lot of type-mismatches in your code such as trying to put an int value where BigDecimal is required. The

How using BigDecimal would affect application performance?

允我心安 提交于 2019-11-29 02:46:01
问题 I want to use BigDecimal to represent arbitrary precision numbers like prices and amounts in a low-latency trading application with thousands of orders and execution reports per second. I won't be doing many math operations on them, so the question is not about performance of the BigDecimal per se, but rather about how large volumes of BigDecimal objects would affect performance of the application. My concern is that huge amount of short-lived BigDecimal objects will put a strain on a GC and

转:BigDecimal 使用方法详解

元气小坏坏 提交于 2019-11-29 02:03:07
转贴:http://zhangyinhu8680.iteye.com/blog/1536397 BigDecimal 由任意精度的整数非标度值 和 32 位的整数标度 (scale) 组成。如果为零或正数,则标度是小数点后的位数。如果为负数,则将该数的非标度值乘以 10 的负 scale 次幂。因此,BigDecimal 表示的数值是 (unscaledValue × 10-scale)。 可以处理任意长度的浮点数运算。 BigDecimal add(BigDecimal val) //加法 BigDecimal subtract (BigDecimal val) //减法 BigDecimal multiply (BigDecimal val) //乘法 BigDecimal divide (BigDecimal val,RoundingMode mode) //除法 具体使用 计算:     加: a.add(b);   减: a.subtract(b);   乘: a.multiply(b);   除: a.divide(b,2);//2为精度取值 除法细解: //注意以下相除会抛出异常,原因: 通过BigDecimal的divide方法进行除法时当不整除,出现无限循环小数时,就会抛异常 //BigDecimal divideBg = a.divide(b); //解决方法是

Convert string to BigDecimal in java

╄→гoц情女王★ 提交于 2019-11-29 01:30:25
问题 I am reading a currency from XML into Java. String currency = 135.69; When I convert this to BigDecimal I get: System.out.println(new BigDecimal(135.69)); Output: 135.68999999999999772626324556767940521240234375. Why is it that it outputs this many numbers? How can I avoid this? All I want is for it to output 135.69. 回答1: The BigDecimal(double) constructor have some problems, is preferrable that you uses BigDecimal(String) or BigDecimal.valueOf(double). System.out.println(new BigDecimal(135

java取整和java四舍五入方法 BigDecimal.setScale()方法详解.

倾然丶 夕夏残阳落幕 提交于 2019-11-29 00:26:09
内容: import java.math.BigDecimal; import java.text.DecimalFormat; public class TestGetInt{ public static void main(String[] args){ double i=2, j=2.1, k=2.5, m=2.9; System.out.println("舍掉小数取整:Math.floor(2)=" + (int)Math.floor(i)); System.out.println("舍掉小数取整:Math.floor(2.1)=" + (int)Math.floor(j)); System.out.println("舍掉小数取整:Math.floor(2.5)=" + (int)Math.floor(k)); System.out.println("舍掉小数取整:Math.floor(2.9)=" + (int)Math.floor(m)); /* 这段被注释的代码不能正确的实现四舍五入取整 System.out.println("四舍五入取整:Math.rint(2)=" + (int)Math.rint(i)); System.out.println("四舍五入取整:Math.rint(2.1)=" + (int)Math.rint(j)); System.out