bigdecimal

Calculating double neighbours of a decimal value

我与影子孤独终老i 提交于 2019-12-24 07:37:41
问题 The code: import java.math.*; public class x { public static void main(String[] args) { BigDecimal a = new BigDecimal(0.1); BigDecimal b = new BigDecimal(0.7); System.out.println(a); System.out.println(b); } } The output: 0.1000000000000000055511151231257827021181583404541015625 0.6999999999999999555910790149937383830547332763671875 This is nice because it lets me find a double that is the closest to given value. But as for 0.1 a value is bigger and for 0.7 value is smaller than real value.

Jooq casting String to BigDecimal

人走茶凉 提交于 2019-12-23 22:29:17
问题 Is there a way to cast a String to a BigDecimal in a jooq-query without losing precision? When i do endResER.VALUE.cast(BigDecimal.class) where VALUE is a field with a String-value in the database it returns a BigDecimal without any fraction digits. I need to compare two amounts that are saved as Strings in the DB. 回答1: You can cast your value to a SQLDataType like this: endResER.VALUE.cast(SQLDataType.DECIMAL.precision(10, 5)) Beware though, that there is a known issue for jOOQ 3.1: #2708.

How to prevent BigDecimal from truncating results?

Deadly 提交于 2019-12-23 19:19:53
问题 Follow up to this question: I want to calculate 1/1048576 and get the correct result, i.e. 0.00000095367431640625. Using BigDecimal 's / truncates the result: require 'bigdecimal' a = BigDecimal.new(1) #=> #<BigDecimal:7fd8f18aaf80,'0.1E1',9(27)> b = BigDecimal.new(2**20) #=> #<BigDecimal:7fd8f189ed20,'0.1048576E7',9(27)> n = a / b #=> #<BigDecimal:7fd8f0898750,'0.9536743164 06E-6',18(36)> n.to_s('F') #=> "0.000000953674316406" <- should be ...625 This really surprised me, because I was under

Avoid the problem with BigDecimal when migrating to Java 1.4 to Java 1.5+

拟墨画扇 提交于 2019-12-23 18:45:54
问题 I've recently migrated a Java 1.4 application to a Java 6 environment. Unfortunately, I encountered a problem with the BigDecimal storage in a Oracle database. To summarize, when I try to store a "7.65E+7" BigDecimal value ( 76,500,000.00 ) in the database, Oracle stores in reality the value of 7,650,000.00 . This defect is due to the rewritting of the BigDecimal class in Java 1.5 (see here). In my code, the BigDecimal was created from a double using this kind of code: BigDecimal myBD = new

default scale of bigdecimal in groovy

喜欢而已 提交于 2019-12-23 12:47:06
问题 What is the default scale of BigDecimal in groovy? And Rounding? So when trying to do calculations: def x = 10.0/30.0 //0.3333333333 def y = 20.0/30.0 //0.6666666667 Base on this, I can assume that it uses scale 10 and rounding half up. Having trouble finding an official documentation saying that though. 回答1: You can find it in the official documentation: The case of the division operator 5.5.1. The case of the division operator The division operators / (and /= for division and assignment)

implementation of BigDecimal in idris

隐身守侯 提交于 2019-12-23 12:03:54
问题 I'm trying to implement a bigdecimal in Idris. I have this so far: -- a big decimal has a numerator and a 10^x value -- it has one type for zero, --TODO the numerator can't be zero for any other case --TODO and it can't be divisible by 10 data BigDecimal : (num : Integer) -> (denExp : Integer) -> Type where Zero : BigDecimal 0 0 BD : (n : Integer) -> (d : Integer) -> BigDecimal n d I would like to force the restrictions marked by "TODO" above. However, I'm am just learning Idris, so I'm not

Very, very large C# floating-point numbers

三世轮回 提交于 2019-12-23 09:15:53
问题 I am doing some population modeling (for fun, mostly to play with the concepts of Carrying Capacity and the Logistics Function). The model works with multiple planets (about 100,000 of them, right now). When the population reaches carrying capacity on one planet, the inhabitants start branching out to nearby planets, and so on. Problem: 100,000+ planets can house a LOT of people. More than a C# Decimal can handle. Since I'm doing averages and other stuff with these numbers, I need the

Android: java rounding error. Can't understand why?

≡放荡痞女 提交于 2019-12-23 08:54:28
问题 Can anybody explain why on Earth these "same" expressions returns different values? (new BigDecimal(String.valueOf(131.7d))).multiply(new BigDecimal(String.valueOf(0.95d))).doubleValue() = 125.115 (new BigDecimal( 131.7d )).multiply(new BigDecimal( 0.95d )).doubleValue() = 125.11499999999998 What BigDecimal is doing different between them? 回答1: If you read the API documentation, you will find taht String.valueOf(dobule) uses Double.toString(double) to format the value. It's perhaps not

Creating a custom BigDecimal type

China☆狼群 提交于 2019-12-23 06:55:41
问题 In my application, all BigDecimal numbers are scaled to have two decimal places.. In other words, everytime I create a new BigDecimal in my code, I need to use the method scale too: BigDecimal x = BigDecimal.ZERO; x.setScale(2, RoundingMode.HALF_UP); So, to minimize the work, I wanted to create my custom BigDecimal type, something like: public class CustomBigDecimal extends BigDecimal { public CustomBigDecimal(String val) { super(val); this.setScale(2, RoundingMode.HALF_UP); } } I know this

BigDecimal保留小数处理

↘锁芯ラ 提交于 2019-12-23 04:33:25
最近在处理支付相关的需求,涉及到金额的问题,采用传统的基本数据类型处理会存在误差,因此采用BigDecimal对象进行处理。 一、构造BigDecimal对象的方式 BigDecimal(int) 创建一个具有参数所指定整数值的对象。 BigDecimal(double) 创建一个具有参数所指定双精度值的对象。 BigDecimal(long) 创建一个具有参数所指定长整数值的对象。 BigDecimal(String) 创建一个具有参数所指定以字符串表示的数值的对象。 注:建议采用BigDecimal(String)进行构造创建BigDecimal对象。 二、BigDecimal对象的加、减、乘、除操作 add(BigDecimal) BigDecimal对象中的值相加,然后返回这个对象。 subtract(BigDecimal) BigDecimal对象中的值相减,然后返回这个对象。 multiply(BigDecimal) BigDecimal对象中的值相乘,然后返回这个对象。 divide(BigDecimal) BigDecimal对象中的值相除,然后返回这个对象。 三、BigDecimal保留小数点问题 1、ROUND_DOWN Rounding mode to round towards zero. 向零方向舍入 示例: 输入数字 使用 DOWN