Inserting BigDecimal numbers into ArrayList in java - java

How can I insert BigDecimal values into an ArrayList using java?
First i declare a bigdecimal number(here it is 0) as shown below.
and after that I need to insert this bigdecimal value 'unit' into an arraylist.My code snippet is shown below.
BigDecimal unit = new BigDecimal(0);
int x=0;
int y=10;
while(x<10){
// Here i need to insert the `BigDecimal` value "unit" into an `ArrayList` for each iteration of this loop
x++;
}

Use List.add:
List<BigDecimal> list = new ArrayList<BigDecimal>();
BigDecimal totalQuantity = new BigDecimal(0);
list.add(totalQuantity);

System.out.println(80.6 + 379.6);
460.20000000000005
System.out.println(BigDecimal(80.6 + 379.6));
460.200000000000045474735088646411895751953125
There are multiple numbers where Java is doing this. Unable to fix and get the programs running correctly, making some of my mathematical programs fail.
Tried in multiple to see if it was a compiler issue.
There may be a bug?

Related

Android Java multiple array initialization

When declaring and assigning primitives before loop everything works fine and could be different from each other later on
//example
double sum1, sum2, sum3, sum4;
sum1 = sum2 = sum3 = sum4 = 0;
//later each gets own value correctly
Is it possible to make oneliner for an array?
//example
double[][] compare, updated; // works as intended
compare = updated = new double[SIZE][]; // makes compare=updated
Problem with second line is that it ignores all following calculations for updated and takes values from compare.
If by "one-liner" you mean a single statement and writing new double[] once, yes you can do this:
double[] arr1, arr2;
arr1 = (arr2 = new double[10]).clone(); // this is the line
arr1[0] = 10;
System.out.println(arr2[0]); // 0.0
But it's not very readable. It gets even worse when you do this with more arrays:
arr1 = (arr2 = (arr3 = new double[10]).clone()).clone();
I suggest you still use multiple lines to do this.
As an alternative to #Sweeper's answer, consider using the Arrays.copyOf() method, as suggested in this answer. Note that the copyOf() method is type-safe whereas the clone() method isn't.
double[] a, b, c;
c = Arrays.copyOf((b = Arrays.copyOf((a = new double[10]), a.length)), b.length);
But again, I'll reiterate as #Sweeper does, that this code really smells and you should consider doing it in multiple lines. As Steve McConnell says in Code Complete 2nd Ed., the Primary Technical Imperative of software is to manage complexity (i.e. make your code simple). This doesn't necessarily mean reducing the lines of code, but more to do with enabling people who read your code to understand what it does at a glance.

Converting large scientific number to long

I've spend a long time now, trying to convert the number 1.2846202978398e+19 in java, without any luck. Currently what I'm trying to do (long)Double.parseDouble(hashes), however this gives 9223372036854775807, which is obviously incorrect. The actual number should look something like this 12855103593745000000.
Using int val = new BigDecimal(stringValue).intValue(); returns -134589568 as it's unable to hold the result. Switching the code to long val = new BigDecimal(hashes).longValue(); gives me -5600541095311551616 which is also incorrect.
I'm assuming this is happening due to the size of a double compared to a long.
Any ideas?
Did you try to use String.format :
String result = String.format("%.0f", Double.parseDouble("1.2846202978398e+19"));
System.out.println(result);
Output
12846202978398000000
Edit
Why you don't work with BigDecimal to do the arithmetic operations, for example :
String str = "1.2846202978398e+19";
BigDecimal d = new BigDecimal(str).multiply(BigDecimal.TEN);
// ^^^^^^^^------example of arithmetic operations
System.out.println(String.format("%.0f", d));
System.out.println(String.format("%.0f", Double.parseDouble(str)));
Output
128462029783980000000
12846202978398000000
Your value exceeds the maximum size of long. You can not use long in this situation.
Try
BigDecimal value = new BigDecimal("1.2846202978398e+19");
After that, you can call
value.toBigInteger()
or
value.toBigIntegerExact()
if needed.
What about:
System.out.println(new BigDecimal("1.2846202978398e+19").toBigInteger());

Java, math (adding, dividing, multiplying) all together

This is my code and it whines about calc 2 and the result.
BigDecimal costNum1 = new BigDecimal(number3.getText().toString());
BigDecimal costNum2 = new BigDecimal(number1.getText().toString());
BigDecimal costNum3 = new BigDecimal(number2.getText().toString());
BigDecimal calc1 = costNum1.multiply(costNum2);
BigDecimal calc2 = calc1.divide("100");
BigDecimal calc3 = calc2.multiply(costNum3);
result.setText(calc3).toString());
Safe to say I'm quite new in this, I'm almost there but I can't make up what is wrong. It's for my first Android App.
BigDecimal#divide accepts another BigDecimal, not a String.
Try
calc2.divide(new BigDecimal("100"));
Also, you have one too many parentheses in your last line.
Try
result.setText(calc3.toString());
You should always count the number of left parens, and see if it matches the number of right parens. If you use an IDE like eclipse, it should point these problems out to you automatically.

How do I use BigDecimal's setScale to get two digits precision

public class Dummy {
public static void main(String args[]) {
String x = "1.234.567,89 EUR";
String e = " EUR";
List<BigDecimal> totals = new ArrayList<BigDecimal>();
totals.add( new BigDecimal(x.replaceAll(" EUR","").replaceAll("\\.","").replaceAll(",",".")));
System.out.println(totals.get(0).add(new BigDecimal(0.10).setScale(3,0)));
}
}
With current code I get 1234567.991 and setting it to setScale(2,0) I get 1234568.00 what I am looking for is 1234567.99. Any help?
Use
System.out.println(totals.get(0).add(new BigDecimal(0.10)
.setScale(2, BigDecimal.ROUND_HALF_UP)));
Out put
1234567.99
I think there must be an inbuilt Java function for this. But otherwise, you can do it with simple mathematics.
You can multiply the number with 100. And then use ceiling or floor function of Java. And then divide it with 100. You got your desired result.
Do not use
new BigDecimal(0.10)
which is both inprecise and does not give a precision/scale of 2.
But use
new BigDecimal("0.10")
This preserves the scale.
This will give desired output.
If you want to go with double at that time we can use DecimalFormat but in bigdecimal setScale is used.
BigDecimal.valueOf(1234567.991).setScale(2, BigDecimal.ROUND_HALF_UP)
.doubleValue();
OUTPUT
1234567.99

Removing the .0 from a double

I am trying to display numbers in a string dynamically, so if the number has decimal's display them but if not don"t show the .0
example: display 5.5 as 5.5 and 5.0 as 5
This is what I have so far: (answer is a double)
double temp = answer;
long temp2 = (long) temp;
if (temp == temp2) {
output = String.valueOf(temp2);
System.out.println(output);
this work's fine up to about 1e18 then will error out because of the maximum size of a Long.
So how would I achieve this on bigger numbers like 5.43e86
Use DecimalFormat
double answer = 5.0;
DecimalFormat df = new DecimalFormat("###.#");
System.out.println(df.format(answer));
The DecimalFormat suggestions are the easiest way to handle this. If they aren't sufficient, here's another idea.
If you're starting to hit the maximum values that can be represented by primitives in Java, then you may need to move to BigInteger and BigDecimal.
Try playing around with the BigDecimal.toBigInteger() method coupled with the toString() methods on BigDecimal and BigInteger.
It's not good solution
if you use new DecimalFormat("0.#") you are missing data, for example
PI = 3.14, but after parse you ae geting 3.1
Another solution to use eval%1 ? (int)d : d
this time couse max integer limit , again missing data
my solution is working, but it's not good idea
res = removeLastChars(eval,".0");
private String removeLastChars(double eval, String text){
String res = String.valueOf(eval);
int length = text.length();
if (res.length() > length){
res = res.substring((res.length() - length), res.length()).equals(text)
? res.substring(0, (res.length() - length)) : res;
}
return res;
}
Look at
http://download.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html
you would want just DecimalFormat("0.0")

Categories