%d is for integers use %f instead, it works for both float and double types:

double d = 1.2;
float f = 1.2f;
System.out.printf("%f %f",d,f); // prints 1.200000 1.200000
Answer from codaddict on Stack Overflow
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Format-double-Java-printf-example
How to format a Java double with printf example
The %f specifier works with both double and float values. The decimal and number after the % specify the number of decimals to print. A comma after the % sign adds a grouping separator for large numbers.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › numberformat.html
Formatting Numeric Print Output (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
There are many converters, flags, and specifiers, which are documented in java.util.Formatter ... The %d specifies that the single variable is a decimal integer. The %n is a platform-independent newline character. The output is: ... The printf and format methods are overloaded.
🌐
W3Schools
w3schools.com › JAVA › ref_output_printf.asp
Java Output printf() Method
assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String Methods
🌐
Oxford University
mathcenter.oxford.emory.edu › site › cs170 › printf
The printf Method
double x = 12.345678; System.out.println("Number is approximately " + (int) (x * 100) / 100.0); But this seems a bit "kludgy". Java provides a better way to do the same thing with the "printf" method: double x = 12.345678; System.out.printf("Number is approximately %.2f", x); //prints "Number is approximately 12.35" System.out.printf( format, item1, item2, ..., itemK ); Here, "format" is a string to print with "format specifiers" (each consisting of a percent sign and a conversion code) that tell Java where and how to display "item1", "item2", etc...
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › How-to-use-Java-printf-to-format-output
Format output with Java printf
The .3 instructs Java printf to limit output to three decimal places. %f is the specifier used to format double and floats with printf.
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › core java › formatting output with printf() in java
Formatting Output with printf() in Java | Baeldung
January 8, 2024 - In this tutorial, we’ll demonstrate different examples of formatting with the printf() method. The method is part of the java.io.PrintStream class and provides String formatting similar to the printf() function in C.
🌐
TutorialsPoint
tutorialspoint.com › article › format-double-type-in-java
Format double type in Java
March 11, 2026 - public class Demo { public static void main(String args[]) { double val1 = 15.546; double val2 = 25.87; double val3 = Math.PI; System.out.format("%f%n", val1); System.out.printf("exp(%.3f) = %.3f%n", val1, Math.exp(val1)); System.out.printf("log = %.3f%n", val1, Math.log(val1)); System.out.format("?%n", val2); System.out.format("%+3f%n", val2); System.out.format("%f%n", val3); System.out.format("%.3f%n", val3); System.out.printf("pow(%.3f, %.3f) = %.3f%n", val1, val2, Math.pow(val1, val2)); } } 15.546000 exp(15.546) = 5643415.357 log = 15.546 25.870000 +25.870000 3.141593 3.142 pow(15.546, 25.870) = 6716991850252629000000000000000.000 · Samual Sam · Updated on: 2026-03-11T22:50:43+05:30 · 3K+ Views · Java - Number doubleValue() Method Java ·
🌐
GeeksforGeeks
geeksforgeeks.org › c language › data-types-in-c
Data Types in C - GeeksforGeeks
#include <stdio.h> int main() { ... sizeof(char)); printf("The size of float: %d\n", sizeof(float)); printf("The size of double: %d", sizeof(double)); return 0; } Output ·...
Published   June 17, 2026
🌐
Wikipedia
en.wikipedia.org › wiki › C_(programming_language)
C (programming language) - Wikipedia
November 10, 2001 - The language does not directly ... (like eval in JavaScript), garbage collection, etc. There are few guards against misuse of language features, which may enable unmaintainable code. In particular, the C preprocessor can hide troubling effects such as double evaluation and ...
🌐
Coderanch
coderanch.com › t › 724364 › java › printf-function
How to use printf %f with a function? (Beginning Java forum at Coderanch)
December 22, 2019 - JavaRanch-FAQ HowToAskQuestionsOnJavaRanch UseCodeTags DontWriteLongLines ItDoesntWorkIsUseLess FormatCode JavaIndenter SSCCE API-17 JLS JavaLanguageSpecification MainIsAPain KeyboardUtility ... Ah! and I thought I had typed in printf. I guess I just needed a break from coding. Thanks again. ... Don't use %1.2f. That means one character with two after the decimal point. If you want to print 123.45 and you are sure you aren't going to have any negative numbers, you want three digits before the decimal point, one decimal point and two after, making six: %6.2f Also don't use doubles for money.
🌐
Reddit
reddit.com › r/learnprogramming › [java] trouble using printf with a double as well as a string
r/learnprogramming on Reddit: [Java] Trouble using printf with a double as well as a string
February 11, 2016 -

I'm trying to make it so that a list of numbers, with 6 dedicated characters, and two 2 decimal figures appears, followed by a "|".

for (int c = 1; c < Config.MAX_VALUE; c++)
    System.out.printf("%6.2f %n",(double)c + "|");

I'm having trouble adding the "|" and get the error

"Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String"

How might I format it properly? Thanks in advance!

🌐
Reddit
reddit.com › r/javahelp › print exact value of double
r/javahelp on Reddit: Print exact value of double
January 7, 2024 -

When I do

System.out.printf("%.50f", 0.3);

it outputs 0.30000000000000000000000000000000000000000000000000 but this can't be right because double can't store the number 0.3 exactly.

When I do the equivalent in C++

std::cout << std::fixed << std::setprecision(50) << 0.3;

it outputs 0.29999999999999998889776975374843459576368331909180 which makes more sense to me.

My question is whether it's possible to do the same in Java?

Top answer
1 of 4
2
I put together an example for you that prints the raw storage bits. I think it covers most cases and should print the stored value as close as possible. For 0.3 it prints 0.29999999999999998889776975374843450 value = 0.3 encoded1 = (5404319552844595 / 4503599627370496) * 2^-2 encoded2 = 1.199999999999999955591079014993738 * 2^-2 result = 0.29999999999999998889776975374843450 import java.math.BigDecimal; import java.math.MathContext; public class DoublePrinter { public static void main(String[] args) { // see https://en.wikipedia.org/wiki/Double-precision_floating-point_format // a double is stored as sign * significand * 2^(exp-1023) double value = 0.3; long bits = Double.doubleToLongBits(value); // the sign is stored in the upmost bit, same as long long sign = Long.signum(bits); // first bit long exponentBits = (bits & EXP_BIT_MASK) >>> 52; // next 11 bits long significandBits = bits & SIGNIF_BIT_MASK; // lowest 52 // add the implicit leading bit if not subnormal if (exponentBits != 0) { significandBits |= 0x10000000000000L; } // map to a more readable represenation (significand is stored as (value / 1L<<52)) long divisor = 1L << 52; int exponent = (int) (exponentBits - 1023); // compute result using BigDecimal var mc = MathContext.DECIMAL128; BigDecimal significand = new BigDecimal(significandBits).divide(new BigDecimal(divisor), mc); BigDecimal scale = new BigDecimal(2).pow(exponent, mc); BigDecimal result = new BigDecimal(sign).multiply(significand).multiply(scale); System.out.println("value = " + value); System.out.println(String.format("encoded1 = %s(%d / %d) * 2^%d", sign < 0 ? "-" : "", significandBits, divisor, exponent)); System.out.println(String.format("encoded2 = %s%s * 2^%d", sign < 0 ? "-" : "", significand.toString(), exponent)); System.out.println("result = " + result); } final static long SIGN_BIT_MASK = 0x8000000000000000L; final static long EXP_BIT_MASK = 0x7FF0000000000000L; final static long SIGNIF_BIT_MASK = 0x000FFFFFFFFFFFFFL; }
2 of 4
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.6 documentation
This syntax is similar to the syntax specified in section 6.4.4.2 of the C99 standard, and also to the syntax used in Java 1.5 onwards. In particular, the output of float.hex() is usable as a hexadecimal floating-point literal in C or Java code, and hexadecimal strings produced by C’s %a format character or Java’s Double.toHexString are accepted by float.fromhex().
🌐
DEV Community
dev.to › realedwintorres › how-to-formatting-java-output-with-printf-1bik
How-To: Formatting Java Output With printf() - DEV Community
November 7, 2021 - You can format other Java data types too. To format an integer type, use %d. To format a floating-point value, use %f. With floating-point formats, you can specify the field width and the number of decimal places. Here is an example: ... public class Example { public static void main(String[] args) { String fruit = "apples"; int quantity = 100; double weight = 36.443; System.out.printf("Item |%s|\nQty |M|\nWeight |.2f|\n", fruit, quantity, weight); } }
🌐
Mkyong
mkyong.com › home › java › java – display double in 2 decimal places
Java - Display double in 2 decimal places - Mkyong.com
October 29, 2021 - It works. System.out.printf("double : %.2f", input); ... Mkyong.com has provided Java and Spring tutorials, guides, and code snippets since 2008.
🌐
Quora
quora.com › Which-format-specifier-is-used-to-print-values-of-double-type-variables
Which format specifier is used to print values of double type variables? - Quora
Answer (1 of 3): The l modifier is required in scanf with double , but not in printf . %Lf (note the capital L ) is the format specifier for long doubles. For plain doubles , either %e , %E , %f , %g or %G will do. The correct printf format for double is %lf
🌐
CodeGym
codegym.cc › java blog › strings in java › printf() in java
Printf() in Java
December 23, 2024 - The value of pi with 3 decimal places is: 3.142 Here we use the printf() method to format and display the value of pi with a specific number of decimal places. The format specifier %.3f indicates that we want to display the floating-point number with three decimal places. Now let’s have another Java printf example in which we print formatted strings and Integers:
🌐
Instanceofjava
instanceofjava.com › 2017 › 03 › java-printf-formatting-table-double.html
Format text using printf() method in java - InstanceOfJava
March 19, 2017 - Also alignment of string data. ... printf() methods ... format double to 2 decimal places in java possible by using system.out.printf() method....
🌐
How to do in Java
howtodoinjava.com › home › java basics › format output with java printf
Format Output with Java Printf
February 23, 2023 - System.out.printf("The int value ... %d %n", BigInteger.TEN); //Prints 'The BigInteger value is 10' Use %f to format float and double values with decimal points....