File: FloatToStr.cpp

package info (click to toggle)
librepfunc 1.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 424 kB
  • sloc: cpp: 1,601; makefile: 270
file content (80 lines) | stat: -rw-r--r-- 2,079 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*******************************************************************************
 * librepfunc - a collection of common functions, classes and tools.
 * See the README file for copyright information and how to reach the author.
 ******************************************************************************/
#include <repfunc.h>
#include <sstream>   // std::stringstream
#include <iomanip>   // std::setfill, std::setw, std::fixed, std::left, std::right


template<class T>
std::basic_string<T> FloatToStrT(double n, size_t width, size_t precision, T fillchar, bool left) {
  std::basic_stringstream<T> ss;

  if (precision) {
     ss.precision(precision);
     ss << std::fixed;
     }

  if (left)
     ss << std::left;
  else
     ss << std::right;

  ss << std::setfill(fillchar) << std::setw(width) << n;
  return ss.str();
}

std::string FloatToStr(double n) {
  return FloatToStrT<char>(n, 0, 0, ' ', true);
}


std::string FloatToStr(double n, size_t width, size_t precision, bool left) {
  return FloatToStrT<char>(n, width, precision, ' ', left);
}


std::wstring FloatToStrW(double n) {
  return FloatToStrT<wchar_t>(n, 0, 0, (wchar_t)' ', true);
}


std::wstring FloatToStrW(double n, size_t width, size_t precision, bool left) {
  return FloatToStrT<wchar_t>(n, width, precision, (wchar_t)' ', left);
}



template<class T>
std::basic_string<T> ExpToStrT(double n, size_t width, size_t precision, T fillchar, bool left) {
  std::basic_stringstream<T> ss;

  if (precision) {
     ss.precision(precision);
     }

  if (left)
     ss << std::left;
  else
     ss << std::right;

  ss << std::scientific << std::setfill(fillchar) << std::setw(width) << n;
  return ss.str();
}

std::string ExpToStr(double n) {
  return ExpToStrT<char>(n, 0, 2, ' ', true);
}

std::string ExpToStr(double n, size_t precision) {
  return ExpToStrT<char>(n, 0, precision, ' ', true);
}

std::wstring ExpToStrW(double n) {
  return ExpToStrT<wchar_t>(n, 0, 2, ' ', true);
}

std::wstring ExpToStrW(double n, size_t precision) {
  return ExpToStrT<wchar_t>(n, 0, precision, ' ', true);
}