Project: Aligning Floating Point with format()
Write a program that demonstrates floating point formatting using default (most compact), fixed point and exponential (scientific) formats using format() style output. Print using no precision, precision of 2 and a field width of 10 with precision 2. Your program should reproduce the output shown using the following variables:
- π = 3.141592653589793
- 𝑒 = 2.718281828459045
- avogadro = 6.0221412927e23
- electron_mass = 9.1093821545e-31
Output
$ javac -Xlint AligningFloatingPointWithFormat.java
$ java -ea AligningFloatingPointWithFormat
General Format
π: 3.14159 3.1 | 3.1|
ℯ: 2.71828 2.7 | 2.7|
Nₐ: 6.02214e+23 6.0e+23 | 6.0e+23|
mₑ: 9.10938e-31 9.1e-31 | 9.1e-31|
Fixed Format
π: 3.141593 3.14 | 3.14|
ℯ: 2.718282 2.72 | 2.72|
Nₐ: 602214129270000000000000.000000 602214129270000000000000.00 |602214129270000000000000.00|
mₑ: 0.000000 0.00 | 0.00|
Scientific Format
π: 3.141593e+00 3.14e+00 | 3.14e+00|
ℯ: 2.718282e+00 2.72e+00 | 2.72e+00|
Nₐ: 6.022141e+23 6.02e+23 | 6.02e+23|
mₑ: 9.109382e-31 9.11e-31 | 9.11e-31|