Try this:
String numberD = String.valueOf(d);
numberD = numberD.substring(numberD.indexOf("."));
Now this numberD variable will have value of 15
Answer from Lucifer on Stack OverflowTry this:
String numberD = String.valueOf(d);
numberD = numberD.substring(numberD.indexOf("."));
Now this numberD variable will have value of 15
Try Math.floor();
double d = 4.24;
System.out.println( d - Math.floor( d ));
To prevent rounding errors you could convert them to BigDecimal
double d = 4.24;
BigDecimal bd = new BigDecimal( d - Math.floor( d ));
bd = bd.setScale(4,RoundingMode.HALF_DOWN);
System.out.println( bd.toString() );
Prints 0.2400
Note that the 4 in setScale is the number of digits after the decimal separator ('.')
To have the remainder as an integer value you could modify this to
BigDecimal bd = new BigDecimal(( d - Math.floor( d )) * 100 );
bd = bd.setScale(4,RoundingMode.HALF_DOWN);
System.out.println( bd.intValue() );
Prints 24
get value after decimal point in java with precision - Stack Overflow
Extracting digit values from before and after decimal points in Java - Stack Overflow
regex - Java - Best way to get numbers after decimal place - Stack Overflow
How to get a certain number of decimal points after the number? java - Stack Overflow
Well, you can use:
double x = d - Math.floor(d);
Note that due to the way that binary floating point works, that won't give you exactly 0.321562, as the original value isn't exactly 4.321562. If you're really interested in exact digits, you should use BigDecimal instead.
Another way to get the fraction without using Math is to cast to a long.
double x = d - (long) d;
When you print a double the toString will perform a small amount of rounding so you don't see any rounding error. However, when you remove the integer part, the rounding is no longer enough and the rounding error becomes obvious.
The way around this is to do the rounding yourself or use BigDecimal which allows you to control the rounding.
double d = 4.321562;
System.out.println("Double value from toString " + d);
System.out.println("Exact representation " + new BigDecimal(d));
double x = d - (long) d;
System.out.println("Fraction from toString " + x);
System.out.println("Exact value of fraction " + new BigDecimal(x));
System.out.printf("Rounded to 6 places %.6f%n", x);
double x2 = Math.round(x * 1e9) / 1e9;
System.out.println("After rounding to 9 places toString " + x2);
System.out.println("After rounding to 9 places, exact value " + new BigDecimal(x2));
prints
Double value from toString 4.321562
Exact representation 4.321562000000000125510268844664096832275390625
Fraction from toString 0.3215620000000001
Exact value of fraction 0.321562000000000125510268844664096832275390625
Rounded to 6 places 0.321562
After rounding to 9 places toString 0.321562
After rounding to 9 places, exact value 0.32156200000000001448796638214844278991222381591796875
NOTE: double has limited precision and you can see representation issue creep in if you don't use appropriate rounding. This can happen in any calculation you use with double esp numbers which are not an exact sum of powers of 2.
You need to multiply by 10, then cast as an int, then apply %10 operation
One easy way to get rid of trailing decimals from a double is to cast it to an int. With some clever casting, we can do this:
double x = 2.546;
x-= (int)x;
x *= 10;
int y = (int) x;
We remove the 1's position (2) from x by subtracting (int) x which is 2, from x which is 2.546. Then we multiply x by 10 to get 5.46. Then by casting x to an int, we get 5. This would work in many other languages.
You can define a function using String#substring and String#indexOf as shown below:
public class Main {
public static void main(String[] args) {
// Tests
System.out.println(getNumberUptoTwoDecimalPlaces("37.348541"));
System.out.println(getNumberUptoTwoDecimalPlaces("-121.88627"));
System.out.println(getNumberUptoTwoDecimalPlaces("-121.8"));
System.out.println(getNumberUptoTwoDecimalPlaces("-121.88"));
System.out.println(getNumberUptoTwoDecimalPlaces("-121.889"));
}
static String getNumberUptoTwoDecimalPlaces(String number) {
int indexOfPoint = number.indexOf('.');
if (indexOfPoint != -1 && number.length() >= indexOfPoint + 3) {
return number.substring(0, indexOfPoint + 3);
} else {
return number;
}
}
}
Output:
37.34
-121.88
-121.8
-121.88
-121.88
For example:
String latitude = "37.348541";
int i = latitude.indexOf(".");
if(i > 0 && i < latitude.length()-2) latitude = latitude.substring(i, i+2);
Here is the code that will print all the digits you mention:
float n = 67.7345f;
System.out.printf("n %% 1= %.4f%n", n % 1);
System.out.printf("n - Math.floor(n) = %.4f%n", n - Math.floor(n));
System.out.printf("n - (int)n= %.4f%n", n - (int)n);
The main point is using %.4f.
Have a look at the sample program output.
But I want the exact 7345
So you can simply use the %.4f.
%.2f will limit to 2 places of decimal.
Any best way to get the exact number after decimal point?
When you are dealing with floating point numbers then there is certainly few issues with the floating point numbers. floatis a 32-bit precision IEEE 754 floating point. A good read What Every Computer Scientist Should Know About Floating-Point Arithmetic
I dont know if thats the best way or not but here is one approach for dealing with rounding of floating point numbers:
public static BigDecimal roundFloat(float x, int roundTodecimalPlace)
{
BigDecimal b = new BigDecimal(Float.toString(x));
b = b.setScale(roundTodecimalPlace, BigDecimal.ROUND_HALF_UP);
return b;
}
Then call it like
float n = 67.7345f;
BigDecimal i;
i=roundFloat(x,4);
You're trying to use string_form before you have actually created it.
If you break
String string_form = new Double(z).toString().substring(0,string_form.indexOf('.'));
double t = Double.valueOf(string_form);
into
String string_temp = new Double(z).toString();
String string_form = string_temp.substring(0,string_temp.indexOf('.'));
double t = Double.valueOf(string_form);
Then it should work.
To get the numbers after the decimal point just take the digits from period until the end of the number.
String string_temp = new Double(z).toString();
String string_form = string_temp.substring(string_temp.indexOf('.'), string_temp.length());
double t = Double.valueOf(string_form);
As others have pointed out though, there are many better ways than converting to string and checking for period and reconverting.
The number of decimal digits before the decimal point is given by
(int)Math.log10(z)+1
The number of decimal digits after it is imprecise and depends on how much precision you use when converting to decimal. Floating-point values don't have decimal places, they have binary places, and the two are incommensurable.
Hi, I started the Helsinki Java MOOC course today and was wondering how to get decimals as a result when dividing? The questions asks: Create a program that asks the user for two integers and prints their quotient. Make sure that 3/2= 1.5
This is what I have:
System.out.println ("Division: 'X / Y = Z' for some integers X, Y and Z");
int X = reader.nextInt();
System.out.println ("Division: 'X / Y = Z' for some integers X, Y and Z");
int Y = reader.nextInt();
int Quotient = X / Y;
String toPrint = X + " / " + Y + " = " + Quotient;
System.out.println(toPrint);
I tried putting some stuff in that empty space to yield some decimals but nothing would work. For example, whenever I run it and input 3/2 I get 1 rather than 1.5
Also, this is my very first day ever trying this stuff and I have 0 background so forgive the lack of proper terms.
.