This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 2 years ago.
While I'm using truncating in this method, it is somewhere wrong as in picture
public static List<Double> interp(DoubleUnaryOperator f, double l, double u, int n) {
double d = (u - l)/n;
List<Double> result = new ArrayList<>(n);
double net = f.applyAsDouble(l);
result.add(net);
int i = 0;
while(i+1 < n){
result.add(Math.floor(f.applyAsDouble(l + d)*100)/100);
l += d;
i++;
}
return result;
}
Who know how to truncate correclty? P.S. its important for the number be in two decimal places.
Use this :
new DecimalFormat("#.##").format(dblVar);
df.setRoundingMode(RoundingMode.DOWN);
s = df.format(d);
and import the class java.text.DecimalFormat.
Related
This question already has answers here:
Sum of the digits of the number 2^1000 [closed]
(11 answers)
Closed 3 years ago.
import java.math.*;
public class PowerDigitSum {
public static void main(String[] args) {
double[] digits ;
digits = new double[302];
double i = Math.pow(2, 1000);
double c = 301;
double c1 = 0;
double d = 0;
while(c>=0) {
c1 = Math.pow(10, c);
d = Math.floor(i/c1);
i = i - d*c1;
digits[(int)c] = (int)d;
c = c-1;
}
double sum = 0;
c = 0;
while (c<302) {
sum = sum+digits[(int)c];
c= c+1;
}
System.out.println(sum);
}
}
The output is 1281 but that's not correct according to projecteuler. What am I doing wrong?
You can't do correct math with double values that large due to their limited nature.
You could probably fix your code with BigDecimal, but it is much easier using BigInteger. Hint: it has a pow method, and you can work out the digit sum starting from toString.
This question already has answers here:
Error with division using double type in Java
(5 answers)
Integer division: How do you produce a double?
(11 answers)
Closed 3 years ago.
Consider the following code snippet:
int[] dataSet = {1,2,3,4};
int total = 0;
for(int temp : dataSet){
total += temp;
}
double mean = (total / dataSet.length);
System.out.println(mean);
I expected this to output 2.5, as that is the mean of 1,2,3,4. Instead, it printed 2.0. Why is this and how can I fix it?
Java 7 or more if you use a int / int you have a int
Try to cast one number to double.
double res = 1 / (double) 2;
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 5 years ago.
So, assuming, for some reason, floats are absolutely required in the programme one is creating despite the inaccuracy regarding decimal points, is there any method one can use to round them to 2 decimal places without converting them into doubles?
Try this:
public static float roundFloat(float number, int scale)
{
int pow = 10;
for (int i = 1; i < scale; ++i)
pow *= 10;
float tmp = number * pow;
return ( (float) ( (int) ((tmp - (int) tmp) >= 0.5f ? tmp + 1 : tmp) ) ) / pow;
}
Solid and fast.
This question already has answers here:
How to remove decimal values from a value of type 'double' in Java
(21 answers)
Closed 6 years ago.
this is my code:
public static void main(String[] args) {
System.out.println("N:");
Scanner in = new Scanner(System.in);
float n = in.nextFloat();
float a,b,c,d;
a = n/1000;
b= n/100%10;
c = n/10%10;
d = n%10;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
and I want to remove those numbers. I'm newbie :) help me pls
Your code seems to try to find the digits of a 4 digit integer number.
Therefore, it makes no sense to use float variables. Use int.
int n = in.nextInt();
int a,b,c,d;
a = n/1000;
b= n/100%10;
c = n/10%10;
d = n%10;
Math.Round(number,2) 2 decimals in this case.
Or, you want the number without decimals? If yes, use int
This question already has answers here:
How to test if a double is an integer
(18 answers)
Closed 8 years ago.
There are several ways to decide if a value is an integer or not.
All of the ways i know are using the divide operation.
Which method is the fastest and:
Is there a method to do this without doing floating point operations?
EDIT: For clarification, my program is only dealing with integer and double values. Here is some of my code:
for (int i = 6;; i++) {
for (int j = i - 1; j > 0; j--) {
double number1 = i;
double number2 = j;
double d = number1 / number2;
int help = (int)d;
if( (d - help) == 0.0){
System.out.println("Whole number found");
}
}
}
Note that int / int will also be an int due to the rules of integral division. You need to promote one of them to floating point or use the idiom 1.0 * a / b if you want to retain a remainder.
If you have a floating point number, f, then
java.lang.Math.floor(f) - f == 0 is probably the best way.
It avoids an intermediate cast to int which can overflow.
This will do what you want!
String str = "1";
try {
Integer.parseInt(str);
} catch (NumberFormatException e) {
System.err.println("Not a number");
}