Get Float part in Int [duplicate] - java

This question already has answers here:
How do I get the part after the decimal point in Java?
(15 answers)
Closed 6 years ago.
I am developing an app in which i have to get the float part in terms of integer value.
for example if my number is 153.12324 then output should be 12324.I tried this way but it works wrong some time.
What's the problem in this or is there a better way to do this ?
there is a double value in d
double d =any_double_value;
String[] splitter = String.valueOf(d).split("\\.");
splitter[0].length(); // Before Decimal Count
int count = splitter[1].length();
int integerpart = (int) calcResult;
double floatpart = calcResult - integerpart;
while (count!=0)
{
floatpart=floatpart*10;
count--;
}
int floatpartinInt=(int)floatpart;
this works wrong only in some cases(it gives 1 number less, like if answer is 124 it gives 123) and in cases where answer is long double no like 3.333333333 (10/3)

You can also without split. by using following way,
double value = 3.25;
double fractionalPart = value % 1;
double integralPart = value - fractionalPart;
This may helps you

Use this code its work in your case and enjoy Man
double val=1.9;
String[] arr=String.valueOf(val).split("\\.");
int[] intArr=new int[2];
intArr[0]=Integer.parseInt(arr[0]); // 1
intArr[1]=Integer.parseInt(arr[1]); // 9

You could also do this:
lvalue = (long) value;
fPart = value - lvalue;

Related

Division in Android Gone Wrong Value [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
When I divided the large number into a small number then the division is correct but when I write the small number to divide a large number the answer returns wrong. In my scenario, the small number always be first. here is my code this code return 7.4074074074074075E-6 but the correct result is 0.0000074074.
double itf = 0.0;
double a = 4.0;
double b = 540000;
itf = a / b;
Log.i(TAG, "savedata: outputvalue=" + itf);
BigDecimal a = new BigDecimal("4");
BigDecimal b = new BigDecimal("540000");
// 0.0000074074
a.divide(b, MathContext.DECIMAL128);
You should use a decimal type. double is outside the scope of support

Why do I get 0.0 when I divide these two integers? [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 2 years ago.
I am trying to show the percentage of day passed using a fixed time. However, when I divide the time passed already by the total amount of time (in seconds) of a day, I get 0.0. I put the current values into the console. Any help is appreciated.
You are performing integer division, and then casting it to a double. You should be doing:
int numOfSecondsSinceMidnight = 61960;
int totalDay = 86400;
double percentDayPassed = 0;
percentDayPassed = (((double)numOfSecondsSinceMidnight / totalDay)*100);
System.out.println(percentDayPassed);
Or better yet, changing numOfSecondsSinceMidnight and totalDay to doubles:
double numOfSecondsSinceMidnight = 61960;
double totalDay = 86400;
double percentDayPassed = 0;
percentDayPassed = ((numOfSecondsSinceMidnight / totalDay)*100);
System.out.println(percentDayPassed);
Both of which print:
71.71296296296296

Why am I getting 0? [duplicate]

This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 5 years ago.
I keep getting 0 with the following equation, and I'm sure it is something I am missing, but this has been bugging me for the past few days.
int BASE_SIZE = 8;
Point screenSize = new Point(1440,2000);
mMaxSize = mScreenSize.x/BASE_SIZE;
// This line is the line causing issue.
int surfaceViewSize = mMaxSize * ((BASE_SIZE-1)/BASE_SIZE);
This is regardless of if I make the variable an integer, if I use Math.round, I make it a double, anything. I can not for the life of me figure this out.
this integer division here:
(BASE_SIZE-1)/BASE_SIZE
result to be
int surfaceViewSize = mMaxSize * 0;
you need to cast one of the operands into a double or float
replace your operations with:
mMaxSize = 1.0*mScreenSize.x/BASE_SIZE;
int surfaceViewSize = mMaxSize * ((1.0*BASE_SIZE-1)/BASE_SIZE);
int surfaceViewSize = (mMaxSize * (BASE_SIZE-1))/BASE_SIZE;
Try this its just a braces issue

Java casting (cast operator expressions) [duplicate]

This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 6 years ago.
The question require me to write a Java program to show the results of the following cast operator expressions:
(double) (23 / 14) + 7.65
My Code:
public class op {
public static void main(String [] args) {
int num = 23/14;
double r1 = (double) num;
double result = r1 + 7.65;
System.out.println("Results: "+ result);
}
}
I don't think I have done correctly, what are the problems of my code?
By the way, can someone tell me what are the differences between long, double, int, float? How do we know when to use these primitive data types? I read an explanation here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
but is there any 'human-version' of the explanation?
Thank you for your help.
The problem is due to the used types.
Since you divide two integers (23 and 14), the result is considered and int as well. Therefor, 23/14 = 1.642857142857143, which is truncated to fit in an int result, more specifically, 1.
result is the sum of 1 (int) and 7.65 (double). Since one of them is a double, to other is converted to the "upper" type as well (double) and the operation becomes 1.0+7.65 = 8.65.
The result is correct, because you asked the result of (double) (23 / 14) + 7.65 which means the result of casting the result of the operations in brackets to double summed with 7.65. Which is 8.65 as previously explained.
If you want to use a division using doubles, consider:
double r1 = 1.0 * 23/14;
Lets see step-by-step:
int num = 23/14; // int division of 23/14 results in 1
So, here num = 1
When you cast num to double value of r1 is setted to 1.0.
double result = r1 + 7.65; //1.0 + 7.65 = 8.65
ok, int is short term for INTEGER which are natural numbers that we use normally but with no decimal places and if your number has some value in between roughly -2 billion to +2 billion. if your range exceeds that and you still want an integer then go for long data type.
floats are for decimal values like 3.147 with a range of +10*38 to -10*38 or so, but if your range exceeds this(practically this happens rarely) go for double.
coming to the code you put here , if you divide a int by another int (like 23/14) you get only get the integer part of the answer(only '1' in 23/14=1.642...) , next when you cast it to double you get 1.0 and next you are going to add that to 7.65 which will make the ultimate answer as 8.65 hope this answers your Q....
You could change this int num = 23/14
to double num = ((double) 23)/14
or double num = (23 * 1.0)/14

Converting double to int [duplicate]

This question already has answers here:
How to cast a double to an int in Java by rounding it down?
(9 answers)
Closed 9 years ago.
Quick question, What is the best way to convert an primitive data type double to a primitive data type int. Currently I am using the following code but it seems to be a lot of methods to do a simple task.
double i = 2.0;
int i2 = Integer.parseInt(Double.toString(i));
just cast double to (int)
double i = 2.0;
int i2 = (int) i;
You could either cast the double to an int:
double i = 2.0
int i2 = (int) i;
or, you could round the number to the nearest whole number, then cast it to an int, just in case you would like to round:
double i = 2.2
int i2 = (int) Math.round(i);
or, you could use
double i = 2.0
int i2 = Integer.parseInt(String.valueOf(i));
About best, though, the first option would be best for the least code, and the second would be better if you would like to get the closest integer to the double.
You can turn any object from one compatible type to another by using 'type-casting'. You use this syntax:
double i = 2.0;
int i2 = (int) i;
Notice how I just put (int) before the variable i? This basically says 'hey, try to make i an int, if you can'. Since java can easily convert a float to an int, it will.
Also you can use Math.floor().
public class Example {
public static void main(String args[]) {
double i = 2.9;
System.out.println((int)(i));
System.out.println((int)Math.floor(i));
System.out.println((int)(-i));
System.out.println((int)Math.floor(-i));
}
}
output:
2
2
-2
-3

Categories