bigdecimal

Using BigDecimal to work with currencies

隐身守侯 提交于 2019-11-26 06:25:12
I was trying to make my own class for currencies using longs, but apparently I should use BigDecimal instead. Could someone help me get started? What would be the best way to use BigDecimal s for dollar currencies, like making it at least but no more than 2 decimal places for the cents, etc. The API for BigDecimal is huge, and I don't know which methods to use. Also, BigDecimal has better precision, but isn't that all lost if it passes through a double ? if I do new BigDecimal(24.99) , how will it be different than using a double ? Or should I use the constructor that uses a String instead?

Convert double to BigDecimal and set BigDecimal Precision

依然范特西╮ 提交于 2019-11-26 05:28:24
问题 In Java, I want to take a double value and convert it to a BigDecimal and print out its String value to a certain precision. import java.math.BigDecimal; public class Main { public static void main(String[] args) { double d=-.00012; System.out.println(d+\"\"); //This prints -1.2E-4 double c=47.48000; BigDecimal b = new BigDecimal(c); System.out.println(b.toString()); //This prints 47.47999999999999687361196265555918216705322265625 } } It prints this huge thing: 47

BigDecimal equals() versus compareTo()

天大地大妈咪最大 提交于 2019-11-26 05:21:52
问题 Consider the simple test class: import java.math.BigDecimal; /** * @author The Elite Gentleman * */ public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub BigDecimal x = new BigDecimal(\"1\"); BigDecimal y = new BigDecimal(\"1.00\"); System.out.println(x.equals(y)); System.out.println(x.compareTo(y) == 0 ? \"true\": \"false\"); } } You can (consciously) say that x is equal to y (not object reference), but when you run the program,

Adding up BigDecimals using Streams

守給你的承諾、 提交于 2019-11-26 05:17:47
问题 I have a collection of BigDecimals (in this example, a LinkedList ) that I would like to add together. Is it possible to use streams for this? I noticed the Stream class has several methods Stream::mapToInt Stream::mapToDouble Stream::mapToLong Each of which has a convenient sum() method. But, as we know, float and double arithmetic is almost always a bad idea. So, is there a convenient way to sum up BigDecimals? This is the code I have so far. public static void main(String[] args) {

Rounding BigDecimal to *always* have two decimal places

不羁岁月 提交于 2019-11-26 04:38:29
问题 I\'m trying to round BigDecimal values up, to two decimal places. I\'m using BigDecimal rounded = value.round(new MathContext(2, RoundingMode.CEILING)); logger.trace(\"rounded {} to {}\", value, rounded); but it doesn\'t do what I want consistently: rounded 0.819 to 0.82 rounded 1.092 to 1.1 rounded 1.365 to 1.4 // should be 1.37 rounded 2.730 to 2.8 // should be 2.73 rounded 0.819 to 0.82 I don\'t care about significant digits, I just want two decimal places. How do I do this with BigDecimal

How to do a fractional power on BigDecimal in Java?

谁都会走 提交于 2019-11-26 04:27:33
问题 In my little project I need to do something like Math.pow(7777.66, 5555.44) only with VERY big numbers. I came across a few solutions: Use double - but the numbers are too big Use BigDecimal.pow but no support for fractional Use the X^(A+B)=X^A*X^B formula (B is the remainder of the second num), but again no support for big X or big A because I still convert to double Use some kind of Taylor series algorithm or something like that - I\'m not very good at math so this one is my last option if

How to set thousands separator in Java?

回眸只為那壹抹淺笑 提交于 2019-11-26 03:55:24
问题 How to set thousands separator in Java? I have String representation of BigDecimal, i want to set thousands separator and return String. 回答1: This should work (untested, based on JavaDoc): DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US); DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols(); symbols.setGroupingSeparator(' '); formatter.setDecimalFormatSymbols(symbols); System.out.println(formatter.format(bd.longValue())); According to the JavaDoc,

How to print formatted BigDecimal values?

南笙酒味 提交于 2019-11-26 03:55:16
问题 I have a BigDecimal field amount which represents money, and I need to print its value in the browser in a format like $123.00 , $15.50 , $0.33 . How can I do that? (The only simple solution which I see myself is getting floatValue from BigDecimal and then using NumberFormat to make two-digit precision for the fraction part). 回答1: public static String currencyFormat(BigDecimal n) { return NumberFormat.getCurrencyInstance().format(n); } It will use your JVM’s current default Locale to choose

Addition for BigDecimal

风流意气都作罢 提交于 2019-11-26 03:52:02
问题 I want to do some simple sums with some currency values expressed in BigDecimal type. BigDecimal test = new BigDecimal(0); System.out.println(test); test.add(new BigDecimal(30)); System.out.println(test); test.add(new BigDecimal(45)); System.out.println(test); Obviously I do not understand well the BigDecimal arithmetics, see output behind. Test 0 0 0 Can anyone help me out? 回答1: The BigDecimal is immutable so you need to do this: BigDecimal result = test.add(new BigDecimal(30)); System.out

The best cross platform (portable) arbitrary precision math library [closed]

空扰寡人 提交于 2019-11-26 03:49:17
问题 I\'m looking for a good arbitrary precision math library in C or C++. Could you please give me some advices / suggestions? The primary requirements: It MUST handle arbitrarily big integers (my primary interest is on integers). In case that you don\'t know what the word arbitrarily big means, imagine something like 100000! (the factorial of 100000). The precision MUST NOT NEED to be specified during library initialization / object creation. The precision should ONLY be constrained by the