bigdecimal

BigDecimal stripTrailingZeros doesn't work for zero

大憨熊 提交于 2019-12-09 15:42:02
问题 I have met strange bug in my code. It relates with new BigDecimal("1.2300").stripTrailingZeros() returns 1.23 (correct) but new BigDecimal("0.0000").stripTrailingZeros() returns 0.0000 (strange), thus nothing happens Why? How to fix it? 回答1: Seems that it is a bug . But it is fixed in Java 8 . Direct URL for fix. There is workaround for this: BigDecimal zero = BigDecimal.ZERO; if (someBigDecimal.compareTo(zero) == 0) { someBigDecimal = zero; } else { someBigDecimal = someBigDecimal

精确小数点后多少位的四种方法

瘦欲@ 提交于 2019-12-09 13:55:34
代码: package com.jc.test; import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberFormat; /** * 精确小数点后多少位的四种方法 * @author wang-xiaoming * */ public class TestPrecision { public static void main(String[] args) { double pi = Math.PI; // 方法一:调用decimalFormat DecimalFormat df = new DecimalFormat(".00"); System.out.println("decimalFormat.format(): " + df.format(pi)); // 方法二:调用String类的format函数 System.out.println("string.format(): " + String.format("%.2f", pi)); // 方法三:调用bigDecimal BigDecimal bd = BigDecimal.valueOf(pi); pi = bd.setScale(2, BigDecimal.ROUND_HALF_UP)

How to get “shortest” BigDecimal that uniquely determines a given double

放肆的年华 提交于 2019-12-08 17:26:47
问题 Basically, I'm curious on how to get hold of new BigDecimal(Double.toString(d)) without going through the process of creating a string. The documentation for Double.toString is quite complex (and interesting). As I understand it, the method does not actually return the string representation of the number actually represented by the given double, but the string representation of the (near by) shortest real number that uniquely identifies the given double . (I don't actually need this. If I did

Adding Bigdecimals inside java 8 stream

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 14:06:46
问题 I would like to know if one way is more effecient than the other. Is there a better java 8 way to do the following operation ? java 8 way BigDecimal total = entries.parallelStream() .map(poec -> BigDecimal.valueOf(poec.getQuantity().longValue() * poec.getAdjustedUnitPrice().doubleValue())) .collect(Collectors.toList()).stream() .reduce(BigDecimal.ZERO, BigDecimal::add); Normal Java 7 way for (final EntryConsumed poec : entries) { total = total.add(BigDecimal.valueOf(poec.getQuantity()

hibernate查询返回DTO对象,DTO封装了多个pojo对象的属性

不羁岁月 提交于 2019-12-08 05:19:39
DTO- 数据传输对象; pojo- 最纯粹的 java 对象与数据库中表的字段一一对应。 简单讲: DTO 起到业务数据的传递作用,数据转换成 JAVA 对象。 1. 查询语句 public void testSqlOneToMany(){ String hql= "select f.id, f.machinenum,f.types_id,t.id as typeId,t.name from gjp_fault f,gjp_type t where f.types_id = t.id" ; SQLQuery query = getSession().createSQLQuery(hql); List<ManyToVo> list = query.setResultTransformer(Transformers. aliasToBean (ManyToVo. class )).list(); System. out .println(list.size()); } 2. 数据封装成对象( DTO ) package bh.test.vo; import java.math.BigDecimal; public class ManyToVo { private BigDecimal ID ; private String MACHINENUM ; //machinenum;

The Java BigDecimal vs The Financial Mans - “The 1 cent problem”

折月煮酒 提交于 2019-12-08 04:30:58
问题 I have this problem: The financial mans says: "Error of your programation" . The official table of values and final calc from financial is base % total value 667.63 - 1.5(0.015) = 657.62 705.98 - 1.5(0.015) = 695.39 687.77 - 1.5(0.015) = 677.45 844.62 - 1.5(0.015) = 831.95 743.23 - 1.5(0.015) = 732.08 775.15 - 1.5(0.015) = 763.52 874.82 - 1.5(0.015) = 861.70 949.63 - 1.5(0.015) = 935.39 987.18 - 1.5(0.015) = 972.37 1040.28 - 1.5(0.015) = 1024.68 1077.70 - 1.5(0.015) = 1061.54 995.68 - 1.5(0

Java 8 Streams map() 示例

怎甘沉沦 提交于 2019-12-07 20:57:28
在Java 8中 stream().map() ,您可以将对象转换为其他对象。查看以下示例: 1.大写字符串列表 1.1简单的Java示例将Strings列表转换为大写。 TestJava8.java package com.mkyong.java8; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class TestJava8 { public static void main (String[] args) { List<String> alpha = Arrays.asList( "a" , "b" , "c" , "d" ); //Before Java8 List<String> alphaUpper = new ArrayList<>(); for (String s : alpha) { alphaUpper.add(s.toUpperCase()); } System.out.println(alpha); //[a, b, c, d] System.out.println(alphaUpper); //[A, B, C, D] // Java 8 List<String>

How can I compare a BigDecimal with ActiveRecord decimal field?

浪尽此生 提交于 2019-12-07 16:07:04
问题 Assume a schema like this: create_table "bills", :force => true do |t| t.decimal "cost", :precision => 10, :scale => 5 end I want to write a function that writes a new bill to the DB iff it is unique. The following does not work: def load_bill_unless_exists(candidate) incumbents = Bill.scoped.where(:cost => candidate.cost) candidate.save unless incumbents.exists? end because the incumbent bills and the candidate bills have different limits in their BigDecimal representation, so the :cost =>

BigDecimal Multiplication

99封情书 提交于 2019-12-07 10:33:02
问题 I try to multiply two BigDecimal Values with multiply methods as follows, BigDecimal dur = BigDecimal.valueOf(60/1.1); BigDecimal bal = BigDecimal.valueOf(1.1); BigDecimal ans = dur.multiply(bal); System.out.println("Ans:"+ans); I am excepting ans as 60 . But i got it as, Ans:59.999999999999994 Why this comming and how can we resolve it. 回答1: The problem is you have a value which can't be represented in double but nor can it be represented in BigDecimal so you have to apply reasonable

How to convert java BigDecimal to normal byte array (not 2's complement)

对着背影说爱祢 提交于 2019-12-07 10:20:35
问题 How do I convert from big integer to a byte array which is not in 2's complement format. Bascially I only need to convert positive numbers and do not need the sign bit. So something like 10 would become a byte 0x0a i.e-> 00001010 [Update] As per comment I tried this public void testBinary() { BigDecimal test = new BigDecimal(35116031); BigInteger theInt = test.unscaledValue(); byte[] arr = theInt.toByteArray(); System.out.println(getCounterVal(arr, new BigInteger("256"))); } public BigInteger