bigdecimal

BigDecimal from Double incorrect value?

£可爱£侵袭症+ 提交于 2019-11-27 09:44:31
I am trying to make a BigDecimal from a string. Don't ask me why, I just need it! This is my code: Double theDouble = new Double(".3"); System.out.println("The Double: " + theDouble.toString()); BigDecimal theBigDecimal = new BigDecimal(theDouble); System.out.println("The Big: " + theBigDecimal.toString()); This is the output I get? The Double: 0.3 The Big: 0.299999999999999988897769753748434595763683319091796875 Any ideas? When you create a double, the value 0.3 cannot be represented exactly. You can create a BigDecimal from a string without the intermediate double, as in new BigDecimal("0.3"

round BigDecimal to nearest 5 cents

↘锁芯ラ 提交于 2019-11-27 08:53:02
I'm trying to figure out how to round a monetary amount upwards to the nearest 5 cents. The following shows my expected results 1.03 => 1.05 1.051 => 1.10 1.05 => 1.05 1.900001 => 1.10 I need the result to be have a precision of 2 (as shown above). Update Following the advice below, the best I could do is this BigDecimal amount = new BigDecimal(990.49) // To round to the nearest .05, multiply by 20, round to the nearest integer, then divide by 20 def result = new BigDecimal(Math.ceil(amount.doubleValue() * 20) / 20) result.setScale(2, RoundingMode.HALF_UP) I'm not convinced this is 100% kosher

工具类

被刻印的时光 ゝ 提交于 2019-11-27 08:28:25
public class CommonUtil implements InputFilter,BaseUtil{ //////////////////////////////////////////////////通用级工具////////////////////////////////////////////////// /** * 颜色类型常量 */ private static final int STYLE_COLOR_1 = 1; private static final int STYLE_COLOR_2 = 2; private static final int STYLE_COLOR_3 = 3; /** * null常量 */ private static final String COMMON_NULL = "null"; private Pattern mPattern; public CommonUtil() { String pattern = "[1-9]{1}\\d{0,8}"; this.mPattern = Pattern.compile(pattern); } /** * 将数据字典list转为Popup可使用的list(判空了) * * @param dataList List<DataDictionaryResponse.ListBean

How can i parse a String to BigDecimal? [duplicate]

馋奶兔 提交于 2019-11-27 07:56:23
This question already has an answer here: Safe String to BigDecimal conversion 9 answers I have this String: 10,692,467,440,017.120 (it's an amount). I want to parse it to a BigDecimal. The problem is that I have tried both DecimalFormat and NumbeFormat in vain. Any help? René Link Try this // Create a DecimalFormat that fits your requirements DecimalFormatSymbols symbols = new DecimalFormatSymbols(); symbols.setGroupingSeparator(','); symbols.setDecimalSeparator('.'); String pattern = "#,##0.0#"; DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols); decimalFormat

Raising a decimal to a power of decimal?

回眸只為那壹抹淺笑 提交于 2019-11-27 07:49:26
The .net framework provides in the Math class a method for powering double. But by precision requirement I need to raise a decimal to a decimal power [ Pow(decimal a, decimal b) ]. Does the framework have such a function? Does anyone know of a library with this kind of function? vappolinario To solve my problem I found some expansion series , and them I had them implemented to solve the equation X^n = e^(n * ln x). // Adjust this to modify the precision public const int ITERATIONS = 27; // power series public static decimal DecimalExp(decimal power) { int iteration = ITERATIONS; decimal result

Format a BigDecimal as String with max 2 decimal digits, removing 0 on decimal part

孤人 提交于 2019-11-27 06:51:40
I have a BigDecimal number and i consider only 2 decimal places of it so i truncate it using: bd = bd.setScale(2, BigDecimal.ROUND_DOWN) Now I want to print it as String but removing the decimal part if it is 0, for example: 1.00 -> 1 1.50 -> 1.5 1.99 -> 1.99 I tried using a Formatter, formatter.format but i always get the 2 decimal digits. How can I do this? Maybe working on the string from bd.toPlainString()? I used DecimalFormat for formatting the BigDecimal instead of formatting the String, seems no problems with it. The code is something like this: bd = bd.setScale(2, BigDecimal.ROUND

Java中四舍五入

大兔子大兔子 提交于 2019-11-27 06:16:24
1、Math中四舍五入的方法 Math.ceil(double a)向上舍入,将数值向上舍入为最为接近的整数,返回值是double类型 Math.floor(double a)向下舍入,将数值向下舍入为最为接近的整数,返回值是double类型 Math.round(float a)标准舍入,将数值四舍五入为最为接近的整数,返回值是int类型 Math.round(double a)标准舍入,将数值四舍五入为最为接近的整数,返回值是long类型 2、Math中random生成随机数 Math.random()生成大于等于0,小于1的随机数 3、Random类生成随机数 两种构造方式:第一种使用默认的种子(当前时间作为种子),另一个使用long型整数为种子,Random类可以生成布尔型、浮点类型、整数等类型的随机数,还可以指定生成随机数的范围 4、BigDecimal处理小数 两种构造方式:第一种直接value写数字的值,第二种用String import java.math.BigDecimal;import java.util.Random;import java.util.concurrent.ThreadLocalRandom;public class TestNumber { public static void main(String[] args){ /

等额本金、等额本息工具类(Java版)

天涯浪子 提交于 2019-11-27 05:59:47
等额本息 / ** * Description:等额本息工具类 * Copyright: Copyright (corporation)2015 * Company: Corporation * @author : 凯文加内特 * @version : 1.0 * Created at: 2015年11月30日 下午3:45:46 * Modification History: * Modified by : */ package com.utils; import java.math.BigDecimal; import java.util.HashMap; import java.util.Map; /** * 等额本息还款,也称定期付息,即借款人每月按相等的金额偿还贷款本息,其中每月贷款利息按月初剩余贷款本金计算并逐月结清。把按揭贷款的本金总额与利息总额相加, * 然后平均分摊到还款期限的每个月中。作为还款人,每个月还给银行固定金额,但每月还款额中的本金比重逐月递增、利息比重逐月递减。 */ public class AverageCapitalPlusInterestUtils { /** * 等额本息计算获取还款方式为等额本息的每月偿还本金和利息 * * 公式:每月偿还本息=〔贷款本金×月利率×(1+月利率)^还款月数〕÷〔(1+月利率)^还款月数-1〕 * *

Efficient BigDecimal round Up and down to two decimals

落花浮王杯 提交于 2019-11-27 05:59:32
问题 In java am trying to find an efficient way to round a BigDecimal to two decimals, Up or Down based on a condition. IF condition true then: 12.390 ---> 12.39 12.391 ---> 12.40 12.395 ---> 12.40 12.399 ---> 12.40 If condition false then: 12.390 ---> 12.39 12.391 ---> 12.39 12.395 ---> 12.39 12.399 ---> 12.39 What is the most efficient way to accomplish this? 回答1: public static BigDecimal round(BigDecimal d, int scale, boolean roundUp) { int mode = (roundUp) ? BigDecimal.ROUND_UP : BigDecimal

ArithmeticException thrown during BigDecimal.divide

元气小坏坏 提交于 2019-11-27 04:35:50
I thought java.math.BigDecimal is supposed to be The Answer™ to the need of performing infinite precision arithmetic with decimal numbers. Consider the following snippet: import java.math.BigDecimal; //... final BigDecimal one = BigDecimal.ONE; final BigDecimal three = BigDecimal.valueOf(3); final BigDecimal third = one.divide(three); assert third.multiply(three).equals(one); // this should pass, right? I expect the assert to pass, but in fact the execution doesn't even get there: one.divide(three) causes ArithmeticException to be thrown! Exception in thread "main" java.lang