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?

Holger
  • 285,553
  • 42
  • 434
  • 765
Ankit
  • 271
  • 3
  • 11

2 Answers2

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

Community
  • 1
  • 1
Devendra Lattu
  • 2,732
  • 2
  • 18
  • 27
  • so why is 0 coming in case of double and random numbers in case of float?? – Ankit Apr 07 '17 at 17:17
  • 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. – Devendra Lattu Apr 07 '17 at 17:49
  • 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. – Devendra Lattu Apr 07 '17 at 17:52
  • @Ankit, I would appreciate if you upvote and mark the answer you found helpful :) Thank you. – Devendra Lattu Apr 07 '17 at 17:53
  • 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. – Ankit Apr 11 '17 at 15:02
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

Anshul Sharma
  • 3,432
  • 1
  • 12
  • 17