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

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.

Related

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());

How to round BigDecimal to return two decimal

I need to return BigDecimal in this format (%.##)
For example i have this floating number
3.2->3.20
6.2->6.20
112.0->112.00
I'm trying this
BigDecimal precoDecial = new BigDecimal(preco);
precoDecial.setScale(2, BigDecimal.ROUND_HALF_UP);
But it is not working for me; this returns:
for:
3.2->3.2000000476837158203125
6.2->6.2
My log with the code reference
BigDecimal precoDecial = new BigDecimal(1.0);
precoDecial = precoDecial.setScale(2, BigDecimal.ROUND_HALF_UP);
System.out.println(precoDecial);
I tried this in my IDE it worked better give it a try hope my work may solve your problem
output
if 1-->1.00
if 1.0-->1.00
if 1.00000000-->1.00

How to add a very small number and a very large number

I am pretty new to Java. I am learning numerical computation at the moment. How does one add and multiply a very small number and a very large number, say something of order $10^{-20}$ and something of order $10^{20}$ to arbitrary precision.
Take a look at the BigDecimal class. From the Javadoc:
Immutable, arbitrary-precision signed decimal numbers.
and:
The BigDecimal class gives its user complete control over rounding behavior.
For your example:
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
BigDecimal big = new BigDecimal("10e20");
BigDecimal small = new BigDecimal("10e-20");
BigDecimal ans = big.add(small);
System.err.println("Answer: " + ans);
}
}
Running gives the following:
$ java Main
Answer: 1000000000000000000000.00000000000000000010
Try the following (didn't count the zeros). You may find other methods to construct 10^20/10^-20 more suitable.
System.out.println( new BigDecimal("0.0000000000000000000000000000001").add( new BigDecimal
("100000000000000000000000000000000")));

Inserting BigDecimal numbers into ArrayList in 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?

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

Categories