Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a number with precision (9,4) ex:4.0000 //4 but that comes on a string without decimal point (40000). So how on java take that string and convert to 4.0000 in double or Bigdecimal?
You can use the BigDecimal(BigInteger, int) constructor. The second argument indicates the scale of the number:
public static BigDecimal convert(String num, int scale) {
BigInteger bi = new BigInteger(num);
return new BigDecimal(bi, scale);
}
Which you can then call like this:
BigDecimal bd = convert("40000", 4);
System.out.println(bd.toPlainString()); // 4.0000
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
Multiplication with the maximum value of the Long type in eclipse results in the following result. I wonder what the result means.
long long1 = 9223372036854775807L;
long long2 = 9223372036854775807L;
int n = 5;
long tmp_long = long1*long2;
System.out.println(long1*n);
System.out.println(tmp_long);
enter image description here
The result is too big to fit in a long, and it overflows, that's why you get those results. If you need to work with such big number correctly you should use the BigInteger class.
BigInteger int1 = BigInteger.valueOf(9223372036854775807L);
BigInteger int2 = BigInteger.valueOf(9223372036854775807L);
BigInteger n = BigInteger.valueOf(5);
System.out.println(int1.multiply(n));
System.out.println(int1.multiply(int2));
Or if you want the overflow to throw an error you could use Math.multiplyExact(long, long)
long long1 = 9223372036854775807L;
long long2 = 9223372036854775807L;
int n = 5;
System.out.println(Math.multiplyExact(long1, n));
System.out.println(Math.multiplyExact(long1, long2));
Java has a BigInteger class to manage such integers.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Suppose there are two HashMaps as follows:
HashMap<String, Integer> h1 = [{"a":1}, {"b":2}, {"c":3}];
HashMap<String, Integer> h2 = [{"k": 1}, {"f": 4}, {"g":5}, {"a":10}]
The multiplication is just like a simple vector multiplication, in this case it will return
1*10 + 2*0 + 3*0 = 10.
That is if the keys are same, then only multiply the two respective values.
Result -> It should return an integer.
int result = 0;
for(String s : h1.keySet()){
if(h2.containsKey(s)){
result = result + h2.get(s) * h1.get(s);
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm using Double.isNaN() to detect NaN value. `
Double nan = Double.NaN;
Double num = 1.5;
Double num2 = 4.5;
Double result = (nan+num)/num2;
System.out.println(result);// the result is NaN
if(Double.isNaN(result))
System.out.println("not NaN");//true
Is there any other way to detect NaN value?
Your condition doesn't correspond to your output - you check if the result is in fact a NaN, but then print that it isn't. Either check that it isn't:
if (!Double.isNaN(result))
System.out.println("not NaN"); // This won't be reached in your case
Or print that it is:
if (Double.isNaN(result))
System.out.println("NaN");
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to round a double value upto 2 decimal places -
double d = 1.13452289575668E8;
DecimalFormat f = new DecimalFormat("##.00");
System.out.println(f.format(d));
OUTPUT: 113452289.58
double d = 2.34568;
DecimalFormat f = new DecimalFormat("##.00");
System.out.println(f.format(d));
OUTPUT: 2.35
So how can I get the double number like 1.13452289575668E8 to correctly display upto 2 decimal places ?
The number is being rounded up correctly
The scientific notation 1.13452289575668E8 means 1.3 x 108 which is 113452289.5756680071353912353515625
double d = 1.13452289575668E8;
System.out.println(new BigDecimal(d));
gives
113452289.5756680071353912353515625
round to 2 decimal places is 113452289.58
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
If I have some initialized integers which are instance variables, for example:
int x = 0;
int y = 0;
Is there a way I could create an array with all of the integers without doing:
int[] array = {x, y};
I would like to not have to add the integers into the array manually.
If the variables are initialized to 0, then just create the array with the appropriate size:
int[] array = new int[10];
All elements in the array are initialized to 0.