Pure Programmer
Blue Matrix


Cluster Map

Project: Aligning Floating Point with printf()

Write a program that demonstrates floating point formatting using default (most compact), fixed point and exponential formats using printf() and/or sprintf() 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
$ node AligningFloatingPointWithPrintf.js General sprintf π: 3.141592653589793 3.1 | 3.1| ℯ: 2.718281828459045 2.7 | 2.7| Nₐ: 6.0221412927e+23 6e+23 | 6e+23| mₑ: 9.1093821545e-31 9.1e-31 | 9.1e-31| Fixed sprintf π: 3.141592653589793 3.14 | 3.14| ℯ: 2.718281828459045 2.72 | 2.72| Nₐ: 6.0221412927e+23 6.0221412927e+23 |6.0221412927e+23| mₑ: 9.1093821545e-31 0.00 | 0.00| Scientific sprintf π: 3.141592653589793e+0 3.14e+0 | 3.14e+0| ℯ: 2.718281828459045e+0 2.72e+0 | 2.72e+0| Nₐ: 6.0221412927e+23 6.02e+23 | 6.02e+23| mₑ: 9.1093821545e-31 9.11e-31 | 9.11e-31|

Solution