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
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 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
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
For instance, I have integers, i.e. 450. I want to get 1/100 of the number N. In this case it should be 4.5, which will rounded to 5.
int i = 450;
int round = Math.round(i/450)?
When the i varies, is this safe always?
i/450 will do an integer division before the result gets passed into Math.round and you won't get what you expected. Even then you got ~1/450 of the value, not 0.01 of it. You need
(int)Math.round(i/100.0);
However you can do rounding with just integer math like either of these
int round = (i + 50)/100; // i + 99 for ceiling
int round = (i - 1)/100 + 1;
int round = i/100 + (i % 100 < 50 ? 0 : 1);
For more information read Rounding integer division (instead of truncating)
See also
How to round up integer division and have int result in Java?
How to Round Up The Result Of Integer Division
Fast ceiling of an integer division in C / C++
These are about ceiling function but you can get the idea
For it to work no matter numerator or denominator, then
BigDecimal.valueOf(450).divide(BigDecimal.valueOf(100),RoundingMode.HALF_UP);
Another way could be like
BigDecimal.valueOf(450,2).setScale(0, RoundingMode.HALF_UP);
You can convert back to int using .intValue()
That would only make sense of course when you are dealing with exact decimal math most of the time until the very end, like would be the case for financial applications.
I "think" this might be what you are looking for:
float i = 450;
int round = Math.round(i/100);
System.out.println(round);//prints 5
Division with int always rounds down so when i is an int, the expression i/100 returns 4 when i is between 400 and 499.
Alternately, you could cast to float:
int i = 450;
int round = Math.round((float)i/100);
System.out.println(round);//prints 5
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 7 years ago.
Improve this question
Say I had double x = 0.0/0.0;.
Is there anything I could do with x in order to get an actual number?
Dividing by itself/0/infinity? Subtracting by something? Anything like that.
You can go through each JLS chapter for each of +, -, *, / and % and you'll read
If either operand is NaN, the result is NaN.
Using the value NaN with any of those would always produce NaN.
Is there anything I could do with x in order to get an actual number?
I'm assuming you meant with the operators above.
I think, we should prevent any number divided by zero instead.
EDIT
avg = 0;
count = 0;
for (number in scores) {
avg += number;
count++;
}
if (count != 0){
return avg / count;
} else {
return 0.0;
}