1

When I am writing this code

float f=56.7876f;
System.out.print(String.format("%32.12f",f)); 

the output is: 56.787601470947

but, when I am writing this code

System.out.print(String.format("%32.12f",56.7876));

the output is: 56.787600000000

Why in both the cases different outputs are being printed despite of the fact that the functionality of both the code is same?

4
  • 2
    56.7876 without the f suffix is a double value. Commented Apr 6, 2017 at 17:51
  • Try 56.7876f... Commented Apr 6, 2017 at 17:51
  • I want to know why in 56.7876f when i am using specific format it is printing 56.787601470947. Why it is not appending the given number with zeroes as in second case when i am using 56.7876 Commented Apr 6, 2017 at 17:59
  • @Ankit, kindly let me know if my solution helps. I would appreciate if you could upvote and mark accepted the solution you found helpful :) Commented Apr 6, 2017 at 18:18

2 Answers 2

1

Referring to why f is placed after float values? now consider this,

    float f = 56.7876f;
    System.out.print(String.format("%32.12f", f));        //                 56.787601470947
    System.out.print(String.format("%32.12f", 56.7876));  //                 56.787600000000
    System.out.print(String.format("%32.12f", 56.7876f)); //                 56.787601470947

For floating point literals the default type is double. When you say, f = 56.7876, the compiler will give warning Type mismatch: cannot convert from double to float. You would need to explicitly type cast it to float (considering the loss of precision from double to float).
In this example the output printed from 56.7876 is of type double 56.787600000000 while the rest are of type float.

To give you a better example, conider the following scenario.

    float f = 56.7874f;
    System.out.print(String.format("%32.12f", f));        //                 56.787399291992
    System.out.print(String.format("%32.12f", 56.7874));  //                 56.787400000000
    System.out.print(String.format("%32.12f", 56.7874f)); //                 56.787399291992

This clearly indicates a loss of precision from 56.7874 to 56.7873

Sign up to request clarification or add additional context in comments.

5 Comments

so why is 0 coming in case of double and random numbers in case of float??
Good question. I would say I am not 100% sure on this, but as per my knowledge, the default datatype for storing floating point numbers in memory if type double. The trailing 0's just indicate the precision that double has being a 64-bit double literal while float is 32-bit float literal.
Say when we add two numbers we just consider the first two floating point values and discard the rest. Float somehow seems to consider the precise values of calculation while saving in memory.
@Ankit, I would appreciate if you upvote and mark the answer you found helpful :) Thank you.
Yes i will upvote answers and i usually do to those answers which clear my doubt. So far nobody is able to tell why random numbers are coming in after decimal case of floating numbers and 0's in case of double values.
0

System.out.print(String.format("%32.12f",56.7876)); it is returns 12 char fractional part filling with 0 and it consider 56.7876 as double.

you can refer following link:- https://dzone.com/articles/java-string-format-examples

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.