bigdecimal

How to change the decimal separator of DecimalFormat from comma to dot/point?

依然范特西╮ 提交于 2019-12-16 20:15:32
问题 I have this little crazy method that converts BigDecimal values into nice and readable Strings. private String formatBigDecimal(BigDecimal bd){ DecimalFormat df = new DecimalFormat(); df.setMinimumFractionDigits(3); df.setMaximumFractionDigits(3); df.setMinimumIntegerDigits(1); df.setMaximumIntegerDigits(3); df.setGroupingSize(20); return df.format(bd); } It however, also produces a so called grouping separator "," that makes all my values come out like this: xxx,xxx I do need the separator

BigDecimal除法运算出现java.lang.ArithmeticException: Non-terminating decimal expansion; no exact represent

≯℡__Kan透↙ 提交于 2019-12-16 07:01:37
  今天在使用两个BigDecimal类型的数字做除法运算时,出现了一个如下的异常信息: 1 java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result   上网查了一下这个异常的,找到了原因所在: 通过BigDecimal的divide方法进行除法时当不整除,出现无限循环小数时,就会抛异常:java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.   解决的办法就是给divide方法设置精确的小数点,如:divide(xxxxx,2)。 来源: CSDN 作者: snxn_ 链接: https://blog.csdn.net/Sunxn1991/article/details/103474502

Java 金额计算不能用float、double!必须用BigDecimal

断了今生、忘了曾经 提交于 2019-12-16 01:56:32
转自: https://www.jianshu.com/p/c9437365702c 金额计算不能用double!!!! 金额计算不能用double!!!! 金额计算不能用double!!!! 重要的事情讲三遍。 先说一下产生丢失精度的原因:浮点数不精确的根本原因在于尾数部分的位数是固定的,一旦需要表示的数字的精度高于浮点数的精度,那么必然产生误差!这在处理金融数据的情况下是绝对不允许存在的。 金额计算必须用BigDecimal,下面对比一下用double 跟BigDecimal的区别,这里为了方便我直接调用工具类MoneyUtil,最后再来看工具类怎么实现 1.出现科学计数法: 1. double d1 = 0.00000000000000001; 2. Log.v("zyl", "d1:"+d1); Log.v("zyl", "d1:"+MoneyUtil.formatMoney(d1+"")); 控制台输出结果:d1:1.0E-17 d1:0.00000000000000001000 2.计算金额不准确,丢失精度: 1. double d1 = 11540; 2. double d2 = 0.35; 3. Log.v("zyl", "result:"+(d1*d2)); 4. Log.v("zyl", "result2:"+MoneyUtil.moneyMul(d1+"",

BigDecimal加减乘除常用运算

心不动则不痛 提交于 2019-12-14 19:24:52
java.math.BigDecimal BigDecimal 在做运算的时候不能直接运算,得调用方法,以下简介: 是不可变的、任意精度的有符号十进制数。 BigDecimal 由任意精度的整数非标度值(unscaledValue)和32位的整数标度(scale)组成。其值为该数的非标度值乘以10的负scale次幂,即为 (unscaledValue * 10-scale) 。 常用方法: 加法: add()函数 减法: subtract()函数 乘法: multiply()函数 除法: divide()函数 绝对值: abs()函数 BigDecimal divideNum = new BigDecimal(100); BigDecimal num = new BigDecimal(20) ; BigDecimal num2 = BigDecimal.TEN; BigDecimal result = BigDecimal.ZERO; result = num.add(num2);//加 result = num.subtract(num2);//减 result = num.multiply(num2);//乘 result = num.divide(divideNum);//除 result = num.divide(divideNum, 2, RoundingMode

Any neat way to limit significant figures with BigDecimal

泪湿孤枕 提交于 2019-12-14 03:45:45
问题 I want to round a Java BigDecimal to a certain number of significant digits (NOT decimal places), e.g. to 4 digits: 12.3456 => 12.35 123.456 => 123.5 123456 => 123500 etc. The basic problem is how to find the order of magnitude of the BigDecimal , so I can then decide how many place to use after the decimal point. All I can think of is some horrible loop, dividing by 10 until the result is <1, I am hoping there is a better way. BTW, the number might be very big (or very small) so I can't

JPA automatic BigDecimal conversion

纵然是瞬间 提交于 2019-12-14 01:28:17
问题 We are generating our jpa access layers with MyEclipse. Afterwards we have the generated models and data layer access services. We ran into some problems for some fields with a defined precision. Entity: @Entity public class TestEntity{ @Column(name="DECTEST", scale = 3, precision = 13) BigDecimal decTest; } Now we create a bean and try to save it: TestEntity te = new TestEntity(); te.setDecTest(new BigDecimal(1.2)); TestEntityService.save(te); We get the following error: Caused by: com.ibm

Why does my sorting method for Bigdecimal numbers fails to sort?

元气小坏坏 提交于 2019-12-13 09:46:08
问题 problem : i employed a certain sorting method, but it didn't work out as i expected and i fail to understand where did i possibly err. My code took input(which are numbers) as Strings into an string array and then converted them into Bigdecimal numbers while i compared them, and then rearranged them accordingly in the array as strings the code in question: String s[]={-100, 50, 0, 56.6, 90, 0.12, .12, 02.34, 000.000}; for(int i=0;i<n-1;i++) { for (int j =i+1; j<n; j++) { BigDecimal d = new

Object with BigDecimals returns empty strings on to_s

倾然丶 夕夏残阳落幕 提交于 2019-12-13 06:06:45
问题 I have a location table I use to store geo coordinates: class Location < ActiveRecord::Base # Location has columns/attributes # BigDecimal latitude # BigDecimal longitude (...) def to_s @latitude.to_s << ', ' << @longitude.to_s end end However, when I call to_s on a Location, the BigDecimal inside converts to an empty string. ruby > l => #<Location id: 1, latitude: #<BigDecimal:b03edcc,'0.4713577E2',12(12)>, longitude: #<BigDecimal:b03ecb4,'-0.7412786E2',12(12)>, created_at: "2011-08-06 03:41

Freemarker format BigDecimal to German Locale not working properly

拈花ヽ惹草 提交于 2019-12-13 03:55:04
问题 Following the documentation of FreeMarker : Using : <#setting number_format="0.##"> <#setting locale="hu"> German people write: ${12345678} Should output as shown in picture but instead what i get is the below : For input new BigDecimal(4436372342.10) => 4 436 372 342,10 which doesn't have . but instead has spaces . Why that happens ? 回答1: Seems like there is a documentation error. locale="hu" sets a Hungarian locale. Its thousands separator is ' ' . To set a German locale, try locale="de_DE"

ByteBuffer to bigdecimal, binary, string

。_饼干妹妹 提交于 2019-12-12 13:31:49
问题 **Please check the edit at the bottom of this post I have a bytebuffer[128 bits] [which has numbers] which I need to convert to bigdecimal, binary, string since these are the corresponding sql mapping while using jdbc. Is there a library API that I can make use of to do this. I see String.valueof() does not take a byte array as a parameter.So I am stuck to doing something like this: BigDecimal bd = new BigDecimal(bigmyBuffer.asCharBuffer().toString()); This looks like a hack to me ?. Is there