Questions tagged [bigdecimal]

BigDecimal is a numeric object type in Java that represents decimal numbers with arbitrary precision.

Filter by
Sorted by
Tagged with
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....
JimArkadian's user avatar
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 ...
onRiv's user avatar
  • 153
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 ...
River Uzoma's user avatar
-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....
Knight Industries's user avatar
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 ...
sameer59's user avatar
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....
Klitos Kyriacou's user avatar
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 - ...
Aaron Bitman's user avatar
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 ...
yoyoyoni1004gmailcom's user avatar
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 ...
jorge jesus achulla palacios's user avatar
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 ...
LXIV's user avatar
  • 21
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"...
Grey's user avatar
  • 11
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 ...
Archana's user avatar
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: --------------------------------------------------------------------ㅍ&...
won's user avatar
  • 73
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 ...
feilong gao's user avatar
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 ...
elvis's user avatar
  • 821
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....
Ranit Das's user avatar
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 ...
Raj Saraogi's user avatar
  • 1,830
-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....
lospalos's user avatar
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 ...
raj's user avatar
  • 15
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” ...
Amalia Muresan's user avatar
-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 ...
leftnut's user avatar
  • 29
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 ...
Carla C's user avatar
  • 109
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 ...
MasterSteve's user avatar
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(&...
Jaya sakthivel's user avatar
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 ...
Andrew Davison's user avatar
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: { ...
Vedant Shah's user avatar
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....
Mr.Simple's user avatar
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,...
jennifer ruurs's user avatar
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 ...
Big McLargeHuge's user avatar
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....
Han's user avatar
  • 63
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 ...
Varun's user avatar
  • 4,352
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 ...
CowOO's user avatar
  • 31
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 ...
MatterOfFact's user avatar
  • 1,313
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 ...
aSemy's user avatar
  • 5,903
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 ...
user21051695's user avatar
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) ? ""...
Alan_P's user avatar
  • 1
-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 ...
Aleyna's user avatar
  • 13
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 ...
James's user avatar
  • 2,984
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....
it.drive.view's user avatar
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 ...
luke's user avatar
  • 11
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... ...
Bob Walsh's user avatar
  • 13.3k
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 ...
JustLudo's user avatar
  • 1,712
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> <...
BuckBazooka's user avatar
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 ...
Vladimir's user avatar
-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&...
Ian Lin's user avatar
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?
Андрей Андрей's user avatar
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. ...
Noname's user avatar
  • 69
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 ...
OutOfMemoryError's user avatar
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 ...
Salanta Rares's user avatar
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 ...
Nementaarion's user avatar

1
2 3 4 5
34