new BigDecimal("37146555.53880000").divide(new BigDecimal("1000000")).scale()
Run Code Online (Sandbox Code Playgroud)
这回来了10.但根据API,divide方法:
返回一个BigDecimal,其值为(this/divisor),其首选标度为(this.scale() - divisor.scale());
所以在这种情况下,37146555.53880000's规模是8,而且1000000规模是0.所以结果应该有一个规模8,而不是10.
我在这里错过了什么?
谢谢
当选择"积极的不正确的集成"或"负的不正确的集成"时,它给出了一个错误,错误是线程"main"中的异常java.lang.ArithmeticException:非终止的十进制扩展; 没有确切的可表示的小数结果.
at java.math.BigDecimal.divide(BigDecimal.java:1603)
at SE_Project_2.calculate(SE_Project_2.java:55)
at SE_Project_2.main(SE_Project_2.java:45)
Run Code Online (Sandbox Code Playgroud)
码:
import java.math.BigDecimal;
import javax.swing.JOptionPane;
public class SE_Project_2 {
private static BigDecimal C0 = new BigDecimal("0.1713245");
private static BigDecimal C1 = new BigDecimal("0.3607616");
private static BigDecimal C2 = new BigDecimal("0.4679139");
private static BigDecimal C3 = new BigDecimal("0.4679139");
private static BigDecimal C4 = new BigDecimal("0.3607616");
private static BigDecimal C5 = new BigDecimal("0.1713245");
private static BigDecimal X0 = new BigDecimal("-0.932469514");
private static BigDecimal X1 = new BigDecimal("-0.661209386");
private static BigDecimal X2 = new BigDecimal("-0.238619186"); …Run Code Online (Sandbox Code Playgroud) 对于BigDecimal,修剪两个以上尾随零的好方法是什么
所以1.2200会打印1.22而1.0000会打印1.00
编辑以及将1.222200作为1.2222和1.220000001返回为1.220000001等.因此,忽略前两个零我想修剪任何传入的0并且不修剪非零值
一种方法可以是乘法,然后应用内置修剪尾随零,然后除以100.它可能是有问题的角落情况但我的问题中的值是基于货币的,并且永远不会超过Java设置的边界(或者它意味着我的软件正在处理以美元计价的出价
丑陋的解决方案如下
System.out.println(((new BigDecimal("1.230223000")
.multiply(new BigDecimal("100.0"))
.stripTrailingZeros()).divide(new BigDecimal("100.0"))));
Run Code Online (Sandbox Code Playgroud) 这应该很简单,但它正在爆炸.有任何想法吗?
d = BigDecimal.new("2.0")
YAML::load({:a => d}.to_yaml)
TypeError: BigDecimal can't be coerced into BigDecimal
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb/inspector.rb:86:in `inspect'
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb/inspector.rb:86:in `inspect'
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb/inspector.rb:86:in `block in <module:IRB>'
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb/inspector.rb:30:in `call'
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb/inspector.rb:30:in `inspect_value'
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb/context.rb:260:in `inspect_last_value'
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb.rb:311:in `output_value'
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb.rb:160:in `block (2 levels) in eval_input'
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb.rb:273:in `signal_status'
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb.rb:156:in `block in eval_input'
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb/ruby-lex.rb:243:in `block (2 levels) in each_top_level_statement'
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `loop'
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `block in each_top_level_statement'
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in `catch'
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in `each_top_level_statement'
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb.rb:155:in `eval_input'
from /Users/benjohnson/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/irb.rb:70:in `block in …Run Code Online (Sandbox Code Playgroud) 我在讨论是使用BigDecimal和BigInteger还是只使用BigDecimal来让我的生活更轻松,更少来回转换.在资源方面仅使用BigDecimal是否存在缺点?
如何仅使用原始数据类型和BigInteger或仅使用BigInteger来使我的生活更轻松,更少来回转换?
什么是最简单的方法,使用最少的不必要的开销来划分两个BigInteger对象并将其存储在BigDecimal对象中?
我一直在努力解决这个问题,找不到任何不使用过多转换的东西.
编辑:意识到这个问题有多愚蠢,我会把它留作参考
我想从java中的bigdecimal中提取整数部分和小数部分.
我正在使用以下代码.
BigDecimal bd = BigDecimal.valueOf(-1.30)
String textBD = bd.toPlainString();
System.out.println("length = "+textBD.length());
int radixLoc = textBD.indexOf('.');
System.out.println("Fraction "+textBD.substring(0,radixLoc)+"Cents: " + textBD.substring(radixLoc + 1, textBD.length()));
Run Code Online (Sandbox Code Playgroud)
我得到的输出为
-1和3
但我希望尾随零也从-1.30开始
输出应为-1和30
我正在尝试执行以下代码:
import java.math.*;
public class HelloWorld{
public static void main(String []args){
System.out.println(BigDecimal.valueOf(Double.NaN));
}
}
Run Code Online (Sandbox Code Playgroud)
合理地说,我得到:
Exception in thread "main" java.lang.NumberFormatException
at java.math.BigDecimal.<init>(BigDecimal.java:470)
at java.math.BigDecimal.<init>(BigDecimal.java:739)
at java.math.BigDecimal.valueOf(BigDecimal.java:1069)
at HelloWorld.main(HelloWorld.java:6)
Run Code Online (Sandbox Code Playgroud)
有没有办法在BigDecimal中表示Double.NaN?
我有一个BigDecimals列表.
List<BigDecimal> amounts = new ArrayList<>()
Run Code Online (Sandbox Code Playgroud)
如何使用Java 8流获取上述列表的摘要统计信息,而不会丢失BigDecimal最多3-4个小数位的精度?
那么,如何使用函数式编程获得此代码的结果:
public static final List<BigDecimal> numbers = Arrays.asList(
new BigDecimal("15"), new BigDecimal("10"), new BigDecimal("17"),
new BigDecimal("30"), new BigDecimal("18"), new BigDecimal("23"),
new BigDecimal("5"), new BigDecimal("12") );
BigDecimal totalOfReducedNumbers = BigDecimal.ZERO;
for(BigDecimal number : numbers) {
if(number.compareTo(BigDecimal.valueOf(20)) > 0)
totalOfReducedNumbers =
totalOfReducedNumbers.add(number.multiply(BigDecimal.valueOf(0.9)));
}
System.out.println("Total of reduced numbers: " + totalOfReducedNumbers);
Run Code Online (Sandbox Code Playgroud)
其中抛出" 总数减少:47.7 "
如何使用 map(),reduce()等函数式编程工具获得相同的结果?
bigdecimal ×10
java ×8
biginteger ×2
java-8 ×2
formatting ×1
java-stream ×1
math ×1
numbers ×1
performance ×1
ruby ×1
yaml ×1