This question already has answers here:
Java Round up Any Number
(7 answers)
Closed 3 years ago.
public class MyClass {
public static void main(String args[]) {
double x=120.38;
System.out.println(Math.round(x));
}
}
Output: 120
But I want an output of 121
Rounding off any decimal values to 1 whole number
Replace Math.round with Math.ceil
Try to use : Math.ceil(x)
It will works for you
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I have a very weird result when I'm using my function and I think that I'm missing something with the rounding and double in java.
For example, when I provide the value 00159,300 for number and 100 for conversion I have 15930,000000000002 which is not possible!
public static String convertMultiply(String number, String conversion) {
number=number.replace(",", ".");
BigDecimal res=BigDecimal.valueOf(Double.valueOf(number)*Integer.valueOf(conversion));
res=res.stripTrailingZeros();
return res.toPlainString().replace(".", ",");
}
thanks in advance!
Double is an approximation of decimal values in Java. Instead, replace your line using double with:
BigDecimal res = (new BigDecimal(number)).multiply(new BigDecimal(conversion));
This question already has answers here:
how to set value of octal in java?
(9 answers)
Closed 2 years ago.
public class Main
{
public static void main(String[] args) {
char a='3';
int b=011;
System.out.println(a+b);
}
}
Output is 60
can someone explain why java behaves like this ?
Literals with a leading zero are octal literals. Any number prefixed with a 0 is considered octal. Octal numbers can only use digits 0-7, just like decimal can use 0-9, and binary can use 0-1. To define integer literals as octal value in Java is effortless.
This question already has answers here:
How do the post increment (i++) and pre increment (++i) operators work in Java?
(14 answers)
Closed 5 years ago.
public class Class1 {
public static void main(String[] args) {
int myFirstNumber =20;
int mySecondNumber=10;
System.out.println(myFirstNumber+++mySecondNumber);
}
}
mySecondNumber should have been incremented to 11, thus making the sum 31
It's the Java parser interpreting
+++
as (myFirstNumber++)+, rather than +(++mySecondNumber)
We use the term greedy to describe that behaviour; i.e. the parser consumes as much of the input as it can in order to form a meaningful expression.
Be assured, that after the println, myFirstNumber will be 21.
This question already has answers here:
"Integer too large" for a small compile time constant
(4 answers)
Closed 5 years ago.
when I add integer number it's show me "integer too large" even if I make it double howto solve this
public class Three {
public static void main(String[] args) {
int i = 05955555;
}
Try this instead:
int i = 5955555;
In Java, an integer number starting with 0 is interpreted as being in octal base - and in that base, you can't have the digit 9.
This question already has answers here:
How does a leading zero change a numeric literal in Java?
(3 answers)
Closed 7 years ago.
Here is some code in java on datatypes:
class Test
{
public static void main(String args[])
{
int i = -0777;
System.out.println(i);
}
}
The output of the above code is -511
If the code is changed to :
class Test
{
public static void main(String args[])
{
int i = -777;
System.out.println(i);
}
}
The output is -777.
Why is the output differing??? What are the calculations done behind this code???
-0777 is treated by the compiler as an octal number (base 8) whose decimal value is -511 (-(64*7+8*7+7)). -777 is a decimal number.