%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%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
Yes, %d means decimal, but it means decimal number system, not decimal point.
Further, as a complement to the former post, you can also control the number of decimal points to show. Try this,
System.out.printf("%.2f %.1f",d,f); // prints 1.20 1.2
For more please refer to the API docs.
Videos
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!
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?