bigdecimal

BigDecimal setScale and round

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: What is the difference between this two call? (Is there any?) // 1. new BigDecimal ( "3.53456" ). round ( new MathContext ( 4 , RoundingMode . HALF_UP )); // 2. new BigDecimal ( "3.53456" ). setScale ( 4 , RoundingMode . HALF_UP ); 回答1: One important point that is alluded to but not directly addressed is the difference between "precision" and "scale" and how they are used in the two statements. "precision" is the total number of significant digits in a number. "scale" is the number of digits to the right of the decimal point. The

Compare if BigDecimal is greater than zero

不打扰是莪最后的温柔 提交于 2019-12-03 02:07:56
问题 How can I compare if BigDecimal value is greater than zero? 回答1: It's as simple as: if (value.compareTo(BigDecimal.ZERO) > 0) The documentation for compareTo actually specifies that it will return -1, 0 or 1, but the more general Comparable<T>.compareTo method only guarantees less than zero, zero, or greater than zero for the appropriate three cases - so I typically just stick to that comparison. 回答2: Possible better way: if (value.signum() > 0) 回答3: Use compareTo() function that's built into

ArithmeticException: “Non-terminating decimal expansion; no exact representable decimal result”

匿名 (未验证) 提交于 2019-12-03 01:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Why does the following code raise the exception shown below? BigDecimal a = new BigDecimal("1.6"); BigDecimal b = new BigDecimal("9.2"); a.divide(b) // results in the following exception. -- java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result. 回答1: From the Java 5 docs (Java 8 docs here ): When a MathContext object is supplied with a precision setting of 0 (for example, MathContext.UNLIMITED), arithmetic operations are exact, as are the arithmetic methods which take no MathContext object.

Java BigDecimal memory usage?

匿名 (未验证) 提交于 2019-12-03 01:54:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is there a guideline for estimating the amount of memory consumed by a BigDecimal ? Looking for something similar to these guidelines for estimating String memory usage. 回答1: If you look at the fields in the source for BigDecimal there is: BigDecimal: long intCompact +8 bytes int precision +4 bytes int scale +4 bytes String stringCache +? BigInteger intVal +? BigInteger: int bitCount +4 bytes int bitLength +4 bytes int firstNonzeroIntNum +4 bytes int lowestSetBit +4 bytes int signum +4 bytes int[] mag +? The comment for stringCache is "Used

Change DecimalFormat locale

冷暖自知 提交于 2019-12-03 01:44:47
I have custom DecimalFormat in Edittext's addTextChangedListener method, everything is working perfectly but when I change language (locale) my addTextChangedListener is not working. double answer = inputDouble * counterToDouble; DecimalFormat df = new DecimalFormat("##.########"); // df=(DecimalFormat)numberFormat; df.setRoundingMode(RoundingMode.DOWN); answer = Double.parseDouble(df.format(answer)); unicoinsAmmount.setText(String.valueOf(df.format(answer))); I searched about my problem and found a NumberFormat solution: NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);

JTable setValueAt StackOverflowError

匿名 (未验证) 提交于 2019-12-03 01:36:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've been searching for whole day and I still can't find simple anwser to my problem: how can I make JTable cell update it's value when I edit it in another one? I wanted to use somehow fireTableCellUpdated but I don't really understand how can I use it, when and on what object. What I want is to obtain is some kind of listener that listens whether that values changes or not. In this particular scenario I have editable third column in which I store the amount and I want that listener to automatically calculate and set values in other cells

sum of BigDecimal List using streams and sum method

匿名 (未验证) 提交于 2019-12-03 01:34:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: If we have all int or long or other primitive datatype value in list then we obtain sun of all values using return items.stream().mapToInt(i -> i).sum(); I have list of BigDecimal values,how to find the sum of all values using Java8 As there is no default method like mapToBigDecimal I tried to create plain map but then I cannot use sum() 回答1: You can use Stream#reduce(BinaryOperator) . A simple example: List<BigDecimal> items = Arrays.asList(BigDecimal.ONE, BigDecimal.valueOf(1.5), BigDecimal.valueOf(100)); items.stream().reduce((i, j) -> i

How to get biggest BigDecimal value

匿名 (未验证) 提交于 2019-12-03 01:33:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How can I get the largest possible value of a BigDecimal variable can hold? (Preferably programmatically, but hardcoding would be ok too) EDIT OK, just realized there is no such thing since BigDecimal is arbitrary precision. So I ended up with this, which is sufficiently good for my purpose: BigDecimal my = BigDecimal.valueOf(Double.MAX_VALUE) 回答1: Its an arbitrary precision class, it will get as large as you'd like until your computer runs out of memory. 回答2: Looking at the source BigDecimal stores it as a BigInteger with a radix, private

Rounding necessary with BigDecimal numbers

匿名 (未验证) 提交于 2019-12-03 00:46:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I want to set scale of two BigDecimal numbers a and b . as in this example : BigDecimal a = new BigDecimal("2.6E-1095"); BigDecimal b = new BigDecimal("2.7E-1105"); int i = 112, j=1; BigDecimal aa = a.setScale(i+j); BigDecimal bb = b.setScale(i+j); and when i run i have this exception: java.lang.ArithmeticException: Rounding necessary at java.math.BigDecimal.divideAndRound(BigDecimal.java:1439) at java.math.BigDecimal.setScale(BigDecimal.java:2394) at java.math.BigDecimal.setScale(BigDecimal.java:2437) Why rounding is necessary ? if i don't

百度开源图表Ecahrt-牛刀小试

匿名 (未验证) 提交于 2019-12-03 00:41:02
echart 百度开源 官网链接: http://echarts.baidu.com/ 1.在demo区选一张图表测试-折柱混合型图表 2.创建pojo-setget方法省略 public class MixLineBarEchart { private EchartLegend legend;//显示项目 private EchartXAxis xAxis;//横坐标 private List<EchartSeries> series;//内容 } public class EchartLegend { private List<String> data; } public class EchartXAxis { private String type;//category private List<String> data;// } public class EchartSeries { private String name; private String type;//bar-柱,line-折线 private List<BigDecimal> data; private int yAxisIndex = 0;//0默认纵坐标左边,1右边纵坐标 } 3.业务注入数据 @RequestMapping("getData") @ResponseBody public String