Adding commas to every thousands of a number

Using std::stringstream, std::locale, and std::numpunct, you can add commas to every thousands of a number as following:

class g3 : public std::numpunct<char> {
protected:
  virtual std::string do_grouping() const { return "\3"; }
};

class Utils
{
public:
  template <typename T>
  static std::string toFormattedString(T num) {
    std::stringstream ss;
    ss.imbue(std::locale(ss.getloc(), new g3));
    ss.precision(2);
    ss << std::fixed << num;
    return ss.str();
  }
};