// Overloaded functions example

#include <iostream>

#include <iomanip>
#include <cmath>
using namespace std;

// specify number of decimal places
void printDouble(double value, int decPlaces = -1);


// specify width and number of decimal places
void printDouble(double value, double format);

int main()
{
    // calls to the first function
    printDouble(0);

    printDouble(1.5);
    printDouble(123456.789);
    printDouble(123456.789,2);
    printDouble(3.141592654,5);

    // calls to the second function
    printDouble(3.14,10.5);
    printDouble(3.14,10.0);
    printDouble(3.14159265359,16.7);
    printDouble(3.14159265359,16.8);
    printDouble(3.14159265359,16.9);
}

void printDouble(double value, int decPlaces)
{
    if (decPlaces < 0) {
        cout << value << endl;
    }
    else {
        cout << setprecision(decPlaces) << fixed << value << endl;
    }
}

// format assumed to be a decimal number representing
// width.decimal_places
void printDouble(double value, double format)
{
    if (format < 0)
        cout << value << endl;
    else
    {
        int width = static_cast<int>(format);
               
        // lround() available in gnu compilers, not available in MS Visual C++

        int decplaces = lround(10.0 * (format - width));
        cout << setw(width) << setprecision(decplaces) << fixed << value << endl;
    }
}

*****  OUTPUT  ******

0
1.5
123457
123456.79
3.14159
   3.14000
         3
       3.1415927
      3.14159265
     3.141592654