Questions tagged [bigdecimal]
BigDecimal is a numeric object type in Java that represents decimal numbers with arbitrary precision.
1,684
questions
1
vote
2
answers
39
views
Precision Error while converting dollars/gallon to dollars/liter and back
The following code has a precision error but I'm at a loss as to how to resolve it. Given an initial value set in dollar/gal of $3.95, when change the ChoiceBox to "dollars/liter" I get $1....
0
votes
0
answers
88
views
Optimize BigDecimal in java
In a web application I concerned, there is a blizzard of BigDecimal operations. Is there any way to optimize this in both memory footprint and cpu util?
I want to write some specific implementations ...
0
votes
2
answers
52
views
How Can I Create A BigDecimal Object that has null as it's value
I have a java program that I am testing and a lot of the code is centered around the idea that a coin can have a value null. The issue, the coin is defined using Coin(BigDecimal) and I cannot figure ...
-2
votes
2
answers
93
views
How to format a BigDecimal without losing trailing fraction zeros?
NumberFormat doesn't seem to care about the scale of a BigDecimal, so given tailing zeros are lost:
NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
nf.setMaximumFractionDigits(Integer....
0
votes
1
answer
89
views
How to implement Excel MROUND() function in Java using BigDecimal instead of double
How can I round to a specific multiple in Java? In excel there is the mround function which allows for easy rounding to a specified multiple like so:
mRound(variable,multiple)
e.g. This is how the ...
1
vote
3
answers
57
views
BigDecimal stripTrailingZeros and equality
Given
BigDecimal a = ...;
BigDecimal b = ...;
Do the following hold for all possible values of a and b?
if (a.compareTo(b) == 0)
assert a.stripTrailingZeros().equals(b.stripTrailingZeros())
if (a....
2
votes
1
answer
77
views
Does BigDecimal division have a shortcoming when the scale is known?
First of all, I should clarify that I'm asking this question only for enlightenment; I'm not desperate for an answer, but an answer might help me appreciate the BigDecimal class more.
I always had - ...
0
votes
1
answer
69
views
Questions about BigDecimal multiply() method in Java
I've been trying to avoid precision loss in case of using BigDecimal multiply() method in Java.
Please check an example code below.
import java.math.BigDecimal;
import java.text.DecimalFormat;
import ...
0
votes
1
answer
50
views
How to add more than two BigDecimals fields to calculate the sum in a By Collector grouping
This is my method where I group the values and perform a sum of all the outstanding balances of the client.
But I have two more values, which I need to calculate the sum:
BigDecimal ...
2
votes
0
answers
54
views
How to format scientific E notation into human readable numbers? (numbers larger than BigDecimal)
I'm trying to create a number class which can hold values from 4.9E-324e-1.7976931348623157E308 to 1.7976931348623157E308e1.7976931348623157E308 in both signs. (yes, the exponent is also a double)
My ...
1
vote
2
answers
65
views
Java Comparator.Comparing().reversed() won't accept BigDecimal Constructor issue
I'm working on a problem where you are given a String[] containing numbers that are all formatted differently - e.g. "9", "-100", "56.6", "0.12", ".12"...
0
votes
1
answer
36
views
BigDecimalRange validator is not working in vaadin 8 (Binder)
BigDecimalRange validator is not working in vaadin version 8.4.3 we tried to change to the latest version of vaadin 8 but the problem persist . Error shows withValidator like renaming the file ,What ...
0
votes
0
answers
25
views
How do I fix the conversion problem to the DecimalNumberWithString function?
I have a problem with converting NSString to NSDecimalNumber.
NSLog(@"calcuMax_test - calculateMaxForCross feeRateDecimal: --------------------------------------------------------------------ㅍ&...
0
votes
2
answers
73
views
How to judge whether the BigDecimal division operation is indivisible in Java?
When we do arithmetic, we often keep the whole decimal if we can divide it completely, and we have to keep a few decimal places if we can't divide it completely.At present, BigDecimal either chooses ...
0
votes
2
answers
237
views
Do I need to have an additional validation for a BigDecimal to check if it is a number and not any string in Java?
I'm working on a Rest API in Java and Spring Boot and I have a POST request and it contains a request body, an object RequestCustomer and one of the field is of type BigDecimal. I need to do a regex ...
0
votes
1
answer
71
views
BigDecimal division precision using string converted to BD
I am trying to convert string values into BigDecimal and then perform my calculation, but the output is not as expected. Please find the code below:
import java.math.BigDecimal;
import java.math....
0
votes
1
answer
47
views
Why BigDecimal.Zero exist in Java where as Double.zero doesn't
I am curious to understand why do we see BigDecimal.Zero but not Double.Zero, so if we need to assign a double variable as Zero then we need to cast it to (double) 0.
Any specific reasons creators ...
-1
votes
2
answers
78
views
Different behaviour of Java BigDecimal.doubleValue() when 14 o 15 decimals
Can someone explain me why BigDecimal.doubleValue() gives me such results:
BigDecimal bd1 = new BigDecimal("38.399999999999999"); //15 decimals
double dd1 = bd1.doubleValue();
dd1 - > 38....
0
votes
0
answers
18
views
Java object to Json node conversion returns String scientific notation of Bigdecimal instead of direct value [duplicate]
Using jackson 2.11, trying to convert java object to JsonNode and it returns scientific notation of Bigdecimal value.
Bigdecimal value - 2000.0, returns scientific value 2e+3 instead I need 2000.0
...
1
vote
3
answers
104
views
convert string to float considering all types of decimal separators
I tried to create a function in java which can convert a string to float considering commas and point as decimal separators and thousand separator can be space, point or commas.
For example “998,4” ...
-1
votes
2
answers
224
views
Facebook warm up challenge that I can't seem to figure out - Battleship
I am working on this MetaCareers code challenge (needs an account):
You're playing Battleship on a grid of cells with 𝑅 rows and 𝐶 columns. There are 0 or more battleships on the grid, each ...
0
votes
4
answers
144
views
Round decimals up to multiples of 5 with BigDecimal
I want to round BigDecimal decimal part according to the following rules:
1710.10 becomes 1710.10
1710.11 becomes 1710.15
1710.15 becomes 1710.15
1710.16 becomes 1710.20
I tried this way new ...
0
votes
2
answers
73
views
How do i give a BigDecimal variable a value in a method?
Like the title says im trying to give the BigDecimal Variable a value of zero "(0)" or any other number in a method. I know that local variables in a method dont have a default value unlike ...
0
votes
0
answers
77
views
Retrieving a Decimal field from Db2 using JDBC
I am trying to retrieve STDDEV of a decimal value from Db2 for z/OS table. I use the below SQL.
// Execute a query and generate a ResultSet instance
rs = stmt.executeQuery(&...
1
vote
3
answers
107
views
Why does this Pi approximation become less accurate?
The code included below calculates a pi approximation in Java using a Monte Carlo method. As expected, the answer (very) gradually improves, but when the main loop terminates the answer is always ...
30
votes
13
answers
43k
views
504 (Outdated Optimize Dep) while using react-vite
I installed a package called big decimal js while using React with JavaScript on Vite. On compiling, it showed the following error on the console, and the application did not load:
My package.json:
{
...
0
votes
1
answer
53
views
Round to 2 decimal places in Java using DecimalFormat
I am using DecimalFormat to round a value(in Double type) to 2 decimal places
var decimalFormat = new DecimalFormat("0.0");
decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
decimalFormat....
3
votes
1
answer
55
views
Issues with bigdecimal
I am trying to calculate leaseRate which is a big decial, However I receive the following error:
Non-terminating decimal expansion; no exact representable decimal result
I see that this is round issue,...
0
votes
1
answer
60
views
How do I use a BigDecimal from Scala.js in my JavaScript application?
I have this Scala object:
@JSExportTopLevel("Calculator")
object Calculator {
@JSExport
def calculate(): BigDecimal = 3.14
}
I can call the exported singleton method from my JavaScript ...
0
votes
2
answers
463
views
BigDecimal from json loses precision
The network call shows that the backend is sending this:
"uom" : "EA",
"qty" : 1.123456789012345678
but when it reaches the frontend, using console.log:
{
qty: 1....
0
votes
0
answers
99
views
Java BigDecimal divide and multiply with same no does not give correct result
I am puzzled by Java Bigdecimal behavior.
Let's say I have dividend = 3 and divisor = 23.
first divide 3/23 = 0.130434783
Once I multiply 0.130434783 * 23 I should get 3.
Bigdicimal gives a different ...
0
votes
1
answer
55
views
Assertion working or failing with BigDecimal depending on the constructor
I am going through JUnit learning and I was doing some basic calculations to see how it works with BigDecimal. Starting with a few values, subtracting them and asserting the final result.
What I found ...
0
votes
0
answers
257
views
Java Mapstruct treat zero value as null
In my mapping, I often have the requirement that 0 values (e.g. BigDecimal.ZERO) should be treated as NULL values.
If it were JavaScript number values, then the requirement would be that all falsy ...
6
votes
1
answer
1k
views
How can I JSON encode BigDecimal and BigInteger in Kotlinx Serialization without losing precision?
I'm using Kotlin/JVM 1.8.0 and Kotlinx Serialization 1.4.1.
I need to encode a java.math.BigDecimal and java.math.BigInteger to JSON.
I'm using BigDecimal and BigInteger because the values I want to ...
1
vote
3
answers
209
views
Descending order of decimals
It is requested to sort the list below from smallest to largest.
Array input: [-100, 50, 0, 56.6, 90, 0.12, .12, 02.34, 000.000]
My Code:
import java.math.BigDecimal;
import java.util.*;
class ...
0
votes
1
answer
73
views
Formatting numbers with NumberFormat and BigDecimal.movePointRight()
I have to format values from a database to display this values in a frontend. The database values looks like this: 0.0009 and the output have to look like this 0,09.
($F{Param1} == null) ? ""...
-1
votes
1
answer
186
views
Java convert/cast object to Double but prevent round?
Object num = 12334555578912349.13;
System.out.println(BigDecimal.valueOf(((Number) num).doubleValue()).setScale(2, BigDecimal.ROUND_HALF_EVEN));
I expect the value to be 12334555578912349.13
but ...
3
votes
0
answers
171
views
How do I set the scale of a BigDecimal stored procedure output param using Hibernate or Spring Data?
Using JDBC, the scale can be set as follows:
callableStatement.registerOutParameter(4, Types.DECIMAL, 34);
Per the javadoc, scale (in case 34) is the desired number of digits to the right of the ...
2
votes
0
answers
51
views
BigDecimal rounding when not knowing desired scale
i know how rounding BigDecimal with the API,
but i actually work witjh a remote API which
sends to me unformated and not rounded BigDecimals.
As examples, the API back-end returns to me
3....
1
vote
2
answers
767
views
Serialise BigDecimal value with customised scale per class field
I have a JSON seriliazer which serializes a BigDecimal in the way presented in this SO answer:
public class MoneySerializer extends JsonSerializer<BigDecimal> {
@Override
public void ...
2
votes
0
answers
876
views
bigdecimal gem 1.4.1 fails to build
Trying to get bigdecimal 1.4.1. or 1.3.5 to compile in a rails project. I get:
bobwalsh@iMac2021 webservices % gem install bigdecimal -v '1.3.5'
Building native extensions. This could take a while...
...
1
vote
1
answer
77
views
DoubleValue of BigDecimal provided wrong output
Using java 11 and Intellij Idea.
I'm in the situation where I get a string of data from an external system and need to reformat this. In the current setup I have this line to extract the value for a ...
0
votes
1
answer
26
views
What is the best way to add BigDecimals in Hadoop?
I need to add BigDecimals in Hadoop. I'm currently using Apache Pig BigDecimalWritable but Pig seems to be completely outdated.
<dependency>
<groupId>org.apache.pig</groupId>
<...
1
vote
1
answer
334
views
How to use Switch-statement inside a Stream
I need to get a balance sum from a list of type List<TransactionSumView>, which I'm receiving from the database.
My TransactionSumView interface:
//projection interface
public interface ...
-2
votes
2
answers
115
views
How to use BigDecimal .equals() in Java? [closed]
import java.math.BigDecimal;
public class test {
public static void main(String[] args) {
BigDecimal b1 = new BigDecimal("0.1");
BigDecimal b2 = new BigDecimal("0.2&...
0
votes
1
answer
99
views
How to substring BigDecimal [duplicate]
I have bigDeciaml format and digits like 14.2345 or 2.567 or 6.65346. How to format bigDeciaml to leave 3 characters after dot?
1
vote
4
answers
1k
views
BigDecimal Java. How to append zeros in front
My question is basically the following:
When I use a value with BigDecimal, how do I append zeros in front of a random number?
Say I want to have a number <10 following an entirely random pattern. ...
0
votes
1
answer
243
views
List of BigDecimal: how to calculate absolute difference between elements not adjacent to Zero value using Java 8 Stream?
I have a list of BigDecimal that could be as elements:
1 = "76.2372"
2 = "0E-4"
3 = "80.2318"
4 = "82.1111"
5 = "88.0937"
I would like to calculate ...
1
vote
0
answers
37
views
Data extracted from excel with too many zecimals
I have some problems when I try to extract from excel from a number format column with 2 decimals precision.
It extracts with too many decimals.
For e.g. : in excel is 2248.40 and when I extract it ...
1
vote
1
answer
3k
views
PySpark cast String to DecimalType without rounding in case of unmatching scale
I need to cast numbers from a column with StringType to a DecimalType. The Decimal type should have a predefined precision and scale, for example, Decimal(2,1). While the numbers in the String column ...