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 Overflow
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 311036 › want-to-print-number-after-decimal-point
java - want to print number after decimal point [SOLVED] | DaniWeb
September 14, 2010 - Please rewording my comments if it is necessary. public class Prinf{ static double decimal(double d){ //a method to return the value after the decimal point int dd = (int)d; // cast into the int data type so that only the integer part is stored ...
Discussions

get value after decimal point in java with precision - Stack Overflow
Let's say I have: double x = 2.546; I need a int that's equal to 5, how would I go about it? More on stackoverflow.com
🌐 stackoverflow.com
Extracting digit values from before and after decimal points in Java - Stack Overflow
I have 2 values String latitude = "37.348541"; String longitude = "-121.88627"; I would like to extract like the values as below with out any rounding up the values. latitude = ... More on stackoverflow.com
🌐 stackoverflow.com
regex - Java - Best way to get numbers after decimal place - Stack Overflow
If you use numWithDecimals0 it will remove the first 2 values and you will just have the decimals :) ... Save this answer. ... Show activity on this post. You can use java.math.BigDecimal to store the data exactly. Then it's possible to print the exact digits after the decimal point. More on stackoverflow.com
🌐 stackoverflow.com
May 22, 2017
How to get a certain number of decimal points after the number? java - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Ask questions, find answers and collaborate at work with Stack Overflow for Teams More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 3
33

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.

2 of 3
29

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.

🌐
Coderanch
coderanch.com › t › 732786 › java › select-digits-decimal-point-Java
How can i select digits after decimal point in Java ? (Java in General forum at Coderanch)
July 20, 2020 - If you're working with strings, use the String.split() method to break the string into 2 parts at the decimal point and use the second part. Note that the argument to split() has to be coded as "\\." because "." is a wildcard match character in regular expressions, and the escaping backslash itself has to be escaped in a Java String literal.
🌐
Coderanch
coderanch.com › t › 504515 › java › decimal
get decimal value (Beginning Java forum at Coderanch)
July 28, 2010 - this topic may be useful too: https://coderanch.com/t/392641/java/java/decimal-part-float you could also get it as float rest=number%1. ... And then just multiply by 10^(number of digits) and round. So to get 3 digits: Another way that will always return you all decimals is using Strings: This ...
Find elsewhere
🌐
javaspring
javaspring.net › blog › how-do-i-get-the-part-after-the-decimal-point-in-java
How to Get the Part After the Decimal Point in Java: Avoiding Precision Errors with Double Variables — javaspring.net
Negative numbers (e.g., -3.25): The decimal part should retain the sign (e.g., -0.25). Use RoundingMode.FLOOR to ensure correct truncation for negatives. Scientific notation: BigDecimal avoids scientific notation by default, so values like 1e-3 (which is 0.001) are handled correctly. Let’s implement the BigDecimal method and test it against edge cases. import java.math.BigDecimal; import java.math.RoundingMode; public class DecimalPartExtractor { public static BigDecimal getDecimalPart(double value) { BigDecimal original = BigDecimal.valueOf(value); // Extract integer part by truncating towa
🌐
YouTube
youtube.com › watch
HOW TO GET ONLY TWO DECIMAL OR POINT VALUE IN JAVA - Intact Abode - YouTube
- Java Swings consultants and developers - Jaspersoft Studio Reports consultants and developersPing me on Skype ID : jysuryam@outlook.comHOW TO GET ONLY TWO ...
Published: January 13, 2018
🌐
CodingTechRoom
codingtechroom.com › question › extract-digits-after-decimal-java
How to Extract Digits After the Decimal Point in Java? - CodingTechRoom
In Java, you can extract the digits following the decimal point from a floating-point number by converting it into a string and splitting the string at the decimal separator. This method provides a simple way to access just the fractional part of the number. ... double number = 12.34567; String[] ...
🌐
Reddit
reddit.com › r/learnjava › how to get decimals on java?
r/learnjava on Reddit: How to get decimals on Java?
October 27, 2018 -

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.

.

🌐
Baeldung
baeldung.com › home › java › java numbers › how to separate double into integer and decimal parts
How to Separate Double into Integer and Decimal Parts | Baeldung
March 17, 2024 - The BigDecimal class in Java provides its user with complete control over rounding behavior. This class also provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. Let’s use BigDecimal to get the integer and decimal parts of a floating point number: double doubleNumber = 24.04; BigDecimal bigDecimal = new BigDecimal(String.valueOf(doubleNumber)); int intValue = bigDecimal.intValue(); System.out.println("Double Number: " + bigDecimal.toPlainString()); System.out.println("Integer Part: " + intValue); System.out.println("Decimal Part: " + bigDecimal.subtract( new BigDecimal(intValue)).toPlainString());