bigdecimal

Java关于数字工具类~持续汇总~

六眼飞鱼酱① 提交于 2019-12-02 04:51:23
. 1描述:求int数组中最大值 /** * 01 * 描述:求int数组中最大值 * 【时间 2019年3月5日下午3:21:36 作者 陶攀峰】 */ public static int test01(int[]sz) { int max = sz[0]; for(int x=1; x<sz.length; x++) { if(sz[x]>max){ max = sz[x]; } } return max; } 2描述:数组排序(从小到大)~传入int数组 .返回int数组. /** * 02 * 描述:数组排序(从小到大)~传入int数组 .返回int数组. * 【时间 2019年3月5日下午3:24:11 作者 陶攀峰】 */ public static int[] test02(int[]sz) { for(int x=0; x<sz.length-1; x++) { for(int y=x+1; y<sz.length; y++) { if(sz[x]>sz[y]) { int temp = sz[x]; sz[x] = sz[y]; sz[y] = temp; } } } return sz; } 3描述:冒泡排序(从小到大)~传入int数组 .返回int数组. /** * 03 * 描述:冒泡排序(从小到大)~传入int数组 .返回int数组. * 【时间

JSF converter with null value

五迷三道 提交于 2019-12-02 02:12:40
问题 I need to convert null property values of type java.math.BigDecimal to 0.00 everywhere with a specified number of decimal places (only for displaying ( <h:outputText> ) thus, not for input components). The basic converter expected to do this job is given below (currency, percentage, locale etc have been excluded completely for brevity). @FacesConverter("bigDecimalConverter") public final class BigDecimalConverter implements Converter { private static final int SCALE = 2; @Override public

JSF converter with null value

廉价感情. 提交于 2019-12-02 02:12:23
I need to convert null property values of type java.math.BigDecimal to 0.00 everywhere with a specified number of decimal places (only for displaying ( <h:outputText> ) thus, not for input components). The basic converter expected to do this job is given below (currency, percentage, locale etc have been excluded completely for brevity). @FacesConverter("bigDecimalConverter") public final class BigDecimalConverter implements Converter { private static final int SCALE = 2; @Override public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) { if (submittedValue

What are the limits of BigDecimal and BigInteger? [duplicate]

南笙酒味 提交于 2019-12-02 01:53:46
问题 This question already has answers here : Is there an upper bound to BigInteger? [duplicate] (3 answers) What does BigInteger having no limit mean? (4 answers) Closed 6 years ago . I was multiplying very two huge BigIntegervalues in a program. It failed. What are the limits of BigInteger and BigDecimal ? 回答1: You won't get NumberFormatException multiplying large numbers. If the number produced is too large, you will get a cryptic NegativeArraySizeException as the size of the array overflows.

Why do immutable classes provide mutators?

拟墨画扇 提交于 2019-12-02 00:50:08
问题 Consider the following code: bdval = new BigDecimal(strval, new MathContext(attrib.getPrecision())); bdval.setScale(attrib.getScale(), RoundingMode.HALF_UP); PMD quite correctly says: Useless operation on Immutable So why do Immutable classes like BigDecimal export mutators for properties? 回答1: setScale() doesn't mutate the BigDecimal it's called on. It returns a copy of the BigDecimal with the new scale value. PMD reports an error because YOUR code is wrong: it ignores the result of the

java中Long精度问题

夙愿已清 提交于 2019-12-01 22:57:15
1、 String price = "19.90"; // 19.99 int totalFee =(int)(new BigDecimal(price).multiply(new BigDecimal("100"))).doubleValue(); int totalFee2 = (int) (Double.parseDouble(price) * 100); System.out.println(totalFee); //1990 System.out.println(totalFee2); //1989 Double 和bigdecimal处理不一样 2、 Long l = 112630689643634688L; 18位的Long,以Long类型返回给前端,前端解析出来的值,不一致。 解决办法: 在response对象里面的Long类型的字段加个注解: 来源: https://www.cnblogs.com/longyao/p/11719884.html

Why do immutable classes provide mutators?

血红的双手。 提交于 2019-12-01 22:27:43
Consider the following code: bdval = new BigDecimal(strval, new MathContext(attrib.getPrecision())); bdval.setScale(attrib.getScale(), RoundingMode.HALF_UP); PMD quite correctly says: Useless operation on Immutable So why do Immutable classes like BigDecimal export mutators for properties? setScale() doesn't mutate the BigDecimal it's called on. It returns a copy of the BigDecimal with the new scale value. PMD reports an error because YOUR code is wrong: it ignores the result of the operation making the operation useless. Your code should be: bdval = bdval.setScale(attrib.getScale(),

Java format BigDecimal numbers with comma and 2 decimal place

亡梦爱人 提交于 2019-12-01 16:08:34
问题 I want to format BigDecimal Numbers with comma and 2 decimal points. e.g. Amount is: 5.0001 and formatted to: 5.00 Amount is: 999999999.999999 and formatted to: 999,999,999.99 Amount is: 1000.4999 and formatted to: 1,000.49 Amount is: 9999.089 and formatted to: 9,999.08 Amount is: 0.19999 and formatted to: 0.19 Amount is: 123456.99999999 and formatted to: 123,456.99 回答1: This should be enough to get the results. String.format("%,.2f", amount.setScale(2, RoundingMode.DOWN)); 回答2: You should

How do I check if a BigDecimal is in a Set or Map in a scale independent way?

醉酒当歌 提交于 2019-12-01 15:43:08
BigDecimal's equals() method compares scale too, so new BigDecimal("0.2").equals(new BigDecimal("0.20")) // false It's contested why it behaves like that. Now, suppose I have a Set<BigDecimal> , how do I check if 0.2 is in that Set, scale independent? Set<BigDecimal> set = new HashSet<>(); set.add(new BigDecimal("0.20")); ... if (set.contains(new BigDecimal("0.2")) { // Returns false, but should return true ... } contains() will work as you want it to if you switch your HashSet to a TreeSet . It is different from most sets as it will decide equality based on the compareTo() method as opposed

Remove the leading 0's till the decimal

回眸只為那壹抹淺笑 提交于 2019-12-01 14:29:21
I want to remove the leading zeros in the decimal numbers. So i want the output should be .324 not 0.324 . I tried str.replaceFirst("^0+(?!$)", ""); Didn't work and i also tried the regex! No results! And yes I am working with BigDecimal. Try this str = str.replaceFirst("^0\\.", "."); if you are trying to format BigDecimal having value 0.324 to .324 . Then below works BigDecimal bd=new BigDecimal("0.23");// this will replace with your BigDecimal DecimalFormat df=null; //check if number is a fraction if(bd.remainder(BigDecimal.TEN).compareTo(BigDecimal.ZERO)>0) df=new DecimalFormat(".###");