bigdecimal

Why is BigDecimal returning a weird value?

好久不见. 提交于 2019-11-30 17:29:29
I am writing code that will deal with currencies, charges, etc. I am going to use the BigDecimal class for math and storage, but we ran into something weird with it. This statement: 1876.8 == BigDecimal('1876.8') returns false. If I run those values through a formatting string "%.13f" I get: "%.20f" % 1876.8 => 1876.8000000000000 "%.20f" % BigDecimal('1876.8') => 1876.8000000000002 Note the extra 2 from the BigDecimal at the last decimal place. I thought BigDecimal was supposed to counter the inaccuracies of storing real numbers directly in the native floating point of the computer. Where is

How can I create a random BigDecimal in Java?

杀马特。学长 韩版系。学妹 提交于 2019-11-30 17:13:10
问题 This question: How to generate a random BigInteger describes a way to achieve the same semantics as Random.nextInt(int n) for BigIntegers. I would like to do the same for BigDecimal and Random.nextDouble(). One answer in the above question suggests creating a random BigInteger and then creating a BigDouble from it with a random scale. A very quick experiment shows this to be a very bad idea :) My intuition is that using this method would require the integer to be scaled by something like n

从个人项目到结对编程

馋奶兔 提交于 2019-11-30 16:48:57
个人项目: 中小学数学卷子自动生成程序 结对编程 项目: 带 UI的小初高数学学习软件 从个人项目到结对编程,可以复用的就只有出题部分,由于个人项目时,出题函数做的并不是很好,好多括号加的没有必要,所以在做结对编程时,对出题函数进行了修改,下面是修改前和修改后的题目比较 接下来进行结对编程的内容,我的搭档负责设计界面和各个界面之间的连接,我负责计算得数等 1.首先将中缀表达式转化为后缀表达式,因为后缀表达式对计算机更友好,方便计算 原理就是:定义一个List用来存后缀表达式,一个栈来存表达式中的操作符,然后遍历字符串:   1.如果为数字,则直接存进List,如果为(,则直接入栈,   2.如果为),则将栈中的内容弹出存入List,直到遇到(,然后弹出(,   3.如果为操作符,如果当前操作符的优先级高于栈顶元素,则直接入栈,否则,弹出栈顶元素到List,知道栈顶元素的优先级低于当前操作符 (PS:优先级的定义为 (最低,其次是"+","-","*","÷",然后是"√","²","sin","cos","tan";) 2.接下来,进行运算  public static String operate(String p,Stack<String> v) 由于double有精度问题和计算机中的存储问题,所以我使用BigDecimal类进行精确运算   1.BigDecimal类的定义

Forcing BigDecimals to use scientific notation

天涯浪子 提交于 2019-11-30 15:33:09
I have this method: public void Example(BigDecimal value, int scale){ BigDecimal x = new BigDecimal("0.00001"); System.out.println("result: " + (value.multiply(x)).setScale(scale, RoudingMode.HALF_UP).toString()); If, per example, value = 1 and scale = 2, the output is "result: 0.00". I thought it would be 1.00E-5. So, my doubt is: How can I force a BigDecimal to be formated in scientific notation if its scale is bigger than a certain value (it was 2 in my example) ? You can use a DecimalFormat with setMinimumFractionDigits(int scale) : private static String format(BigDecimal x, int scale) {

data type to represent a big decimal in java

回眸只為那壹抹淺笑 提交于 2019-11-30 14:52:10
Which data type is apt to represent a decimal number like "10364055.81". If tried using double: double d = 10364055.81; But when I try to print the number, its displaying as " 1.036405581E7 ", which I don't want. Should I use BigDecimal? But its displaying as 10364055.81000000052154064178466796875 . Is there any datatype that displays the values as it is? Also the number may be bigger than the one taken as example. BTW, will using BigDecimal effect the performance of the application?? I might use this in almost all my DTOs. You should use BigDecimal - but use the String constructor , e.g.: new

除法运算时的一个常见异常之java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.

痞子三分冷 提交于 2019-11-30 12:47:32
一、背景 今天在计算库存消耗百分比(消耗的库存/总库存)的时候遇到了一个错误, java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result. 通过异常的描述,我们知道这是因为,某些场景下对于如1/3会得到一个无穷小数,这个时候需要定义计算结果要保留到小数点后几位,否则就会抛出上面的异常。 二、方法介绍 出现异常时使用的方法,此方法没有精度设置。 public BigDecimal divide(BigDecimal divisor) 在进行除法运算的时候,我们需要使用下面的方法来进行精度控制。 public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) 附:不要忘记判断分母是否为0    三、代码如下 BigDecimal b1 = new BigDecimal(1); BigDecimal b2 = new BigDecimal(3); if (!Objects.equals(b2, BigDecimal.ZERO)) { // 不能整除,数学上是无穷小数,抛出ArithmeticException异常 //BigDecimal b3 = b1

How can I get more than 100 decimal digits in C#?

不打扰是莪最后的温柔 提交于 2019-11-30 12:13:27
Is it possible to get more than 100 decimal digits in C#? If yes what is the necessary portion of code? In Java there something call BigDecimal but it still can't reach more than 55 digits. Using J# libraries: Download the J# Redistributable to obtain the J# class libraries. This article hints about how to use it to get ZIP-ability in your .NET projects, and explains a bit about BigDecimal, but as Ramhound explained, the article is relatively old. After download, add the vsjlib to your project. It didn't work for me on .NET 4.0, but it did work on 2.0 project. It contains a BigDecimal class,

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

橙三吉。 提交于 2019-11-30 08:27:17
问题 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

Convert string to BigDecimal in java

╄→尐↘猪︶ㄣ 提交于 2019-11-30 07:52:33
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. The BigDecimal(double) constructor have some problems, is preferrable that you uses BigDecimal(String) or BigDecimal.valueOf(double). System.out.println(new BigDecimal(135.69)); //135.68999999999999772626324556767940521240234375 System.out.println(new BigDecimal("135.69")); // 135

How to multiply a BigDecimal by an integer in Java

只谈情不闲聊 提交于 2019-11-30 06:02:32
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; } You have a lot of type-mismatches in your code such as trying to put an int value where BigDecimal is required. The corrected version of your code: public class Payment { BigDecimal itemCost = BigDecimal.ZERO; BigDecimal