[go: up one dir, main page]

Menu

[r1123]: / trunk / src / formatfloat.h  Maximize  Restore  History

Download this file

79 lines (56 with data), 2.0 kB

 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
#ifndef GUARD_formatfloat_h
#define GUARD_formatfloat_h
#include <iostream>
#include <cmath>
#include <sstream>
#include <iomanip>
using namespace std;
namespace mesmer
{
template<typename T>
void formatFloat( ostream& out, // Output Stream.
const T& datum, // Floating point value.
const int precision, // Required precision.
const int width // Required width.
){
ostringstream sstrdatum ;
sstrdatum << setprecision(precision) << datum ;
string thisString = sstrdatum.str();
sstrdatum.clear();
// insert a '0' if there is only one zero after the +/- sign
string::size_type position = thisString.find('e', 0);
if (position != string::npos){
string sub = thisString.substr(position + 2);
if (sub.length() == 2){
thisString.insert(position + 2, 1, '0');
}
}
ostringstream sstrdatum1 ;
sstrdatum1 << thisString;
out.setf(ios::right, ios::adjustfield) ;
out << setw(width) << sstrdatum1.str() ;
}
template<typename T>
string formatFloat(const T& datum, // Floating point value.
const int precision, // Required precision.
const int width // Required width.
){
ostringstream sstrdatum ;
sstrdatum << setprecision(precision) << datum ;
string thisString = sstrdatum.str();
sstrdatum.clear();
// insert a '0' if there is only one zero after the +/- sign
string::size_type position = thisString.find('e', 0);
if (position != string::npos){
string sub = thisString.substr(position + 2);
if (sub.length() == 2){
thisString.insert(position + 2, 1, '0');
}
}
ostringstream sstrdatum1 ;
sstrdatum1.setf(ios::right, ios::adjustfield) ;
sstrdatum1 << setw(width) << thisString ;
return sstrdatum1.str();
}
}
#endif //GUARD_formatfloat_h