bigdecimal

Decimal expansion program running very slow for large inputs

可紊 提交于 2019-12-10 13:58:51
问题 I am writing a program to calculate the decimal expansion on the number 103993/33102 and I want to print out all of the trailing decimals depending on what number the user inputs. It runs quickly for all number up to 10^5 but if input 10^6 to program takes around 5 minutes to print out an answer. How can I speed things up? I have tried two different approaches one using BigDecimal and the other using strings and neither one is working efficiently. public static void main(String[] args) throws

Replace grouping separator of DecimalFormat in formatted value

南笙酒味 提交于 2019-12-10 13:55:11
问题 I've used DecimalFormat df = new DecimalFormat("#,###.00"); to format a BigDecimal . Now, I want to use that formatted value (say it is '1 250,00') to create new BigDecimal . I've tried this: BigDecimal result = new BigDecimal(model.getValue().replace(",",".").replace(" ","")); But that space between 1 and 2 in 1 250.00 is not replaced. How can I fix it? Example: DecimalFormat df = new DecimalFormat("#,###.00"); BigDecimal example = new BigDecimal("1250"); String str = df.format(example);

浮点数之间的等值判断,基本数据类型不能用==来比较,包装数据类型不能用 equals 来判断。

老子叫甜甜 提交于 2019-12-10 13:41:45
【强制】浮点数之间的等值判断,基本数据类型不能用==来比较,包装数据类型不能用 equals 来判断。 说明:浮点数采用“尾数+阶码”的编码方式,类似于科学计数法的“有效数字+指数”的表示方式。 二进 Java 开发手册 8/44 制无法精确表示大部分的十进制小数,具体原理参考《码出高效》。 反例: float a = 1.0f - 0.9f; float b = 0.9f - 0.8f; if (a == b) { // 预期进入此代码快,执行其它业务逻辑 // 但事实上 a==b 的结果为 false } Float x = Float.valueOf(a); Float y = Float.valueOf(b); if (x.equals(y)) { // 预期进入此代码快,执行其它业务逻辑 // 但事实上 equals 的结果为 false } 正例: (1) 指定一个误差范围,两个浮点数的差值在此范围之内,则认为是相等的。 float a = 1.0f - 0.9f; float b = 0.9f - 0.8f; float diff = 1e-6f; if (Math.abs(a - b) < diff) { System.out.println("true"); } (2) 使用 BigDecimal 来定义值,再进行浮点数的运算操作。 BigDecimal a =

Clarification on behavior of BigDecimal.stripTrailingZeroes()

喜夏-厌秋 提交于 2019-12-10 13:13:00
问题 Why the following code prints 0.00 and not 0 ? BigDecimal big = new BigDecimal("0.00"); big = big.stripTrailingZeros(); System.out.println(big.toPlainString()); The following is the documentation for stripTrailingZeroes: Returns BigDecimal which is numerically equal to this one but with any trailing zeros removed from the representation. For example, stripping the trailing zeros from the BigDecimal value 600.0, which has [BigInteger, scale] components equals to [6000, 1], yields 6E2 with

Why is bundle installing the wrong version for bigdecimal using 1.2.0?

亡梦爱人 提交于 2019-12-10 11:58:10
问题 I'm using Bundle to install gems in d:/cucumbertests/cucumber/ successfully, which installs BigDecimal v 1.1.0. This failed in d:/cucumbertests/cucumber/itinfix which installed BigDecimal v 1.2.0. How do I identify the correct version of BigDecimal in my Gemfile in d:/cucumbertests/cucumber/itinfix ? Why is the version of BigDecimal correct in d:/cucumbertests/cucumber ? The latest version is 1.2.0, found by running gem query --remote --name-matches bigdecimal is: REMOTE GEMS: bigdecimal (1.2

Sorting number values stored as a BigDecimal type with Spring Data MongoDB in MongoDB

谁都会走 提交于 2019-12-10 10:56:02
问题 I'm studying Spring Data MongoDB with a tutorial (https://spring.io/guides/tutorials/data/2/). In this tutorial, the type of cost is BigDecimal as following and it is stored as a "string" in MongoDB. So, when I tried to get a sort result with an ascending sort by the cost field, I got a wrong result. I found that using the BigDecimal class is the best way for calculations. But, if I save numbers as a BigDecimal type with Spring Data MongoDB in MongoDB, it will be saved as a string type and I

BigDecimal Method returns vague results, why does this happen?

ぐ巨炮叔叔 提交于 2019-12-10 10:54:19
问题 Code: package tk.vivekpatani.www; import java.math.BigDecimal; import java.util.Scanner; public class SimpleCalc { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); try{ System.out.println("Welcome to the Calculator!"); System.out.println("Enter Number 1:"); double num1 = sc.nextDouble(); System.out.println("Enter Number 2:"); double num2 = sc.nextDouble(); BigDecimal bd1 = new BigDecimal(num1); BigDecimal bd2 = new BigDecimal

How to handle rounding errors in Java's BigDecimal

為{幸葍}努か 提交于 2019-12-10 10:35:37
问题 I'm working with open source project (axil) that implements a scripting engine inside of java applications and I've hit a major stumbling block while trying to utilize BigDecimal's rounding. It seems that BigDecimal is converting my input to scientific notation and then applying my passed in precision to the coefficient of the SN representation of the number, rather than to its non-SN representation. For example: new BigDecimal("-232454.5324").round(new MathContext(2, RoundingMode.HALF_UP))

division of bigdecimal by integer

别等时光非礼了梦想. 提交于 2019-12-10 06:56:12
问题 I want to divide a bigdecimal value with an integer.i have rounded the bigdecimal value(if it is 133.333 then rounded value is 133).given below is my code snippet. v1 = v1.setScale(0, RoundingMode.HALF_UP); int temp = BigDecimal.valueOf(v1.longValue()).divide(constant1); value of constant is 12. It is showing an error message that The method divide(BigDecimal) in the type BigDecimal is not applicable for the arguments (int) Can anyone help me to do the division? 回答1: Change .divide(constant1)

hibernate returning BigDecimal datatype instead of long

旧巷老猫 提交于 2019-12-10 04:17:22
问题 The hibernate named query returns a BigDecimal for a column that has datatype NUMBER. select col1 as "col1" from table1 union select col2 as "col1" from table2 On client side, I expect the datatype of col1 to be long (primitive) I do this: <return-scalar column="col1" type="java.lang.Long" /> or <return-scalar column="col1" type="long" /> In both cases, I get : java.lang.ClassCastException: java.math.BigDecimal incompatible with java.lang.Long How can I fix this? My suspiscion, something