This question already has answers here:
How do I get the part after the decimal point in Java?
(15 answers)
Closed 6 years ago.
int number = 24.24;
int afterDot = (int) (number*100)%100;
afterDot = 24
This logic is wrong if
int number1 = 24.4
How can I get the 4 from 24.4 ?
Whatever will be the number but want to extract the value after dot.
Actually the formula has to work for both type of value.
If you convert your double to a string, the problem becomes easier :
double number = 24.4;
String numberAsString = String.valueOf(number);
String decimalPart = numberAsString.split("\\.")[1];
System.out.println(decimalPart);
int number1 = Integer.valueOf(decimalPart); // NOTE: This conversion is lossy.
System.out.println(number1);
Note that by converting your decimalPart string (e.g. "001") to an integer (1), you might lose some information.
With 24.4, it outputs :
4
4
With 24.001, it outputs :
001
1
With 3d, it outputs:
0
0
Related
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 3 years ago.
when i want to get the result of 100/100000..
i got only 0.
Example
int one = 100;
int two = 100000;
int result = one/two;
toast(result); //Result is 0
Hey there "int" data type only stores integer values and not the decimals.
So if you divide 3 with 2 you would get 1 as answer instead of 1.5 .
Int just ignores the decimals .
You need to choose float or double data type for this to work.
Your variable named result must be declared and casted to float data type.
Appreciate the effort and mark this as answer if it helps you.....
This question already has answers here:
percentage of two int?
(4 answers)
Closed 5 years ago.
I have such code:
int number1 = 20;
int number2 = number1/100;
I want to get percentage of number1 as 0,2 -> I suppose int should be converted into some other type. But I need 0,2 to use for BigDecimal number. How can I get this 0,2 from int number1 = 20?
int number1 = 20 has to be in that type.
Will be grateful for help.
Using a BigDecimal is appropriate here. You can use BigDecimal.valueOf in order to convert int to BigDecimal. Then, you can divide by specifying a scale and a rounding mode.
Example :
System.out.println(
BigDecimal.valueOf(20).divide(BigDecimal.valueOf(100), 1, RoundingMode.HALF_UP)
);
Will display :
0.2
With a different scale :
System.out.println(
BigDecimal.valueOf(20).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP)
);
It will display :
0.20
Or like stated in the question comments, you can also create directly the BigDecimal from a String :
System.out.println(new BigDecimal("0.2"));
Divide the int by a floatvalue to give to it a floating number representation and assign it to a floating number.
float number2 = number1/100F;
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
I have the number "159.82" and I need to get it to print just "82"
What I have been trying is something along the lines of
double total = 159.82;
System.out.println(total - (int) total);
But that leaves me with 0.8199999999999 so clearly I am doing something wrong.
Simple way: Turn it into an String, use indexOf to find the ".", use substring to print 82.
Would this fit what you're looking for? It outputs the decimal value, rounded to two places, as a String.
double total = 159.82;
String decimalValue = String.format("%.2f", total).split("\\.")[1];
System.out.println(decimalValue);
(Prints "82")
Due to how float/double works, the easiest solution could be to work with strings. This will return the decimals of any double input.
double input = 159.82;
String decimals = Double.toString(input);
decimals = decimals.substring(decimals.lastIndexOf('.') + 1);
System.out.println(decimals);
// prints "82"
final int fraction = Integer.valueOf((total+"").replaceFirst("-?\\d+\\.", ""));
System.out.println( fraction );
Another solution with replaceAll, so you can replace everything until the dot :
double total = 159.82;
System.out.println(String.valueOf(total).replaceAll("^.*\\.", ""));// output = 82
This can work either with negative numbers for example :
double total = -159.82; // output = 82
Another solution if you want to get only 2 numbers then you can use :
double total = 159.82; // positive or negative
int result = Math.abs((int) (total * 100) % 100);
System.out.println(result); // output = 82
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Double decimal formatting in Java
(14 answers)
Closed 6 years ago.
Suppose, I have a double value.
double dble = 5.91742691
Now can I get that value with two digits after the point. I mean to say,can I get 5.91 from there programmatically?
One more, suppose, I want to get an integer from a double value if the double value is X.9XXXXX. Here I mean to say, I want to compare the AFTER-POINT value. For your understanding here dble's AFTER-POINT value is 91742691. How can I do that?
If you want to truncate a double to 2 decimal places, do
double twoDecDbl = (int)(dble * 100) / 100.0; // 5.91
or
double twoDecDbl = Math.floor(dble * 100) / 100; // 5.91
However, if you want to get the numbers after the decimal place as an integer (which I don't know why you would want to do this), then do
long decimals = Long.parseLong(("" + dble).split("\\.")[1]); // 91742691
Note: The maximum value of an int is 2,147,483,647 (10 digits there), but a double can hold 16 digits after the decimal point, so a long must be used in order to stay safe (can have 19 digits). Alternatively, you can just keep it as a String by removing the wrapping parse.
For #1 Try it
NumberFormat formatter = new DecimalFormat("#0.00");
String s = formatter.format(dble);
double num = Double.valueOf(s);
or
dble = Math.floor(dble * 100) / 100;
For #2 Try it
double num = dble - (int)dble;
In case Integer.MIN_VALUE < dble < Integer.MAX_VALUE
This question already has answers here:
Why does the division of two integers return 0.0 in Java? [duplicate]
(6 answers)
Double value returns 0 [duplicate]
(3 answers)
Closed 9 years ago.
Why does Java return a 0 when I divide 10/60?
the code I tried is
double pay_per_minute = (10/60);
10 being the pay rate per hour and 60 being the minutes.
Because you're building an integer. When you store it in a double variable, it's already too late : it's 0.
Do
double pay_per_minute = (10.0/60);
If you have variables, cast them :
double pay_per_minute = ((double)pay_per_hour) / 60;
any primitive digit in java is treated as an integer, so when you do 10/60, it is integer division and due to precision loss it is giving 0
Here 10 and 60 takes as int values and then you get int dividing result it is 0 then you get answer as 0. use following way.
double a=10;
double b=60;
double div=a/b;
you need to type cast it first because by default numericals are considered as integers
double pay_per_minute = ((double)10/60);
System.out.println(pay_per_minute);
output 0.16666666666666666
double pay_per_minute = (10/60);
Here, you are dividing integer 10 by integer 60. So, this is like doing
int temp = 10/60;
double pay_per_minute = double(temp)
your temp will be 0 (since 10/60 is 0 when considered as integer division)
You need to do,
double pay_per_minute = (10.0/60);