bigdecimal

Logarithm of a BigDecimal

亡梦爱人 提交于 2019-11-26 02:57:19
问题 How can I calculate the logarithm of a BigDecimal? Does anyone know of any algorithms I can use? My googling so far has come up with the (useless) idea of just converting to a double and using Math.log. I will provide the precision of the answer required. edit: any base will do. If it\'s easier in base x, I\'ll do that. 回答1: Java Number Cruncher: The Java Programmer's Guide to Numerical Computing provides a solution using Newton's Method. Source code from the book is available here. The

Safe String to BigDecimal conversion

混江龙づ霸主 提交于 2019-11-26 02:36:18
问题 I\'m trying to read some BigDecimal values from the string. Let\'s say I have this String: \"1,000,000,000.999999999999999\" and I want to get a BigDecimal out of it. What is the way to do it? First of all, I don\'t like the solutions using string replaces (replacing commas etc.). I think there should be some neat formatter to do that job for me. I\'ve found a DecimalFormatter class, however as it operates through double - huge amounts of precision are lost. So, how can I do it? 回答1: Check

Square root of BigDecimal in Java

时光怂恿深爱的人放手 提交于 2019-11-26 02:35:41
问题 Can we compute the square root of a BigDecimal in Java by using only the Java API and not a custom-made 100-line algorithm? 回答1: I've used this and it works quite well. Here's an example of how the algorithm works at a high level. Edit: I was curious to see just how accurate this was as defined below. Here is the sqrt(2) from an official source: (first 200 digits) 1

What is the equivalent of the Java BigDecimal class in C#?

孤街浪徒 提交于 2019-11-26 01:59:24
问题 BigDecimal is a class in the java.math package that has a lot of benefits for handling big numbers of a certain scale. Is there an equivalent class or data type in c# with this feature. 回答1: C# only has BigInteger built it (in .NET framework 4). Is decimal enough precision for your task? It's a 128-bit number that can hold values in the range ±1.0 × 10 −28 to ±7.9 × 10 28 . 回答2: Just recently I also needed an arbitrary precision decimal in C# and came across the idea posted here: https:/

Using BigDecimal to work with currencies

笑着哭i 提交于 2019-11-26 01:57:30
问题 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

ArithmeticException: “Non-terminating decimal expansion; no exact representable decimal result”

亡梦爱人 提交于 2019-11-26 00:39:40
问题 Why does the following code raise the exception shown below? BigDecimal a = new BigDecimal(\"1.6\"); BigDecimal b = new BigDecimal(\"9.2\"); a.divide(b) // results in the following exception. -- java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result. 回答1: From the Java 8 docs: When a MathContext object is supplied with a precision setting of 0 (for example, MathContext.UNLIMITED), arithmetic operations are exact, as are the arithmetic methods

Representing Monetary Values in Java [closed]

99封情书 提交于 2019-11-26 00:30:14
I understand that BigDecimal is recommended best practice for representing monetary values in Java. What do you use? Is there a better library that you prefer to use instead? ninesided BigDecimal all the way. I've heard of some folks creating their own Cash or Money classes which encapsulate a cash value with the currency, but under the skin it's still a BigDecimal , probably with BigDecimal.ROUND_HALF_EVEN rounding. Edit: As Don mentions in his answer , there are open sourced projects like timeandmoney , and whilst I applaud them for trying to prevent developers from having to reinvent the

Double vs. BigDecimal?

不想你离开。 提交于 2019-11-25 23:02:47
问题 I have to calculate some floating point variables and my colleague suggest me to use BigDecimal instead of double since it will be more precise. But I want to know what it is and how to make most out of BigDecimal ? 回答1: A BigDecimal is an exact way of representing numbers. A Double has a certain precision. Working with doubles of various magnitudes (say d1=1000.0 and d2=0.001 ) could result in the 0.001 being dropped alltogether when summing as the difference in magnitude is so large. With