scifir-units
2.0.0
scifir-units is a library of units of measurement, angles, coordinates, fields, and related data, all lightweight, that help in the development of scientific software and scientific machines
Loading...
Searching...
No Matches
util
types.cpp
Go to the documentation of this file.
1
#include "
./types.hpp
"
2
3
#include <cmath>
4
#include <sstream>
5
6
using namespace
std;
7
8
namespace
scifir
9
{
10
float
parse_float
(
const
string
& x)
11
{
12
try
13
{
14
float
value =
stof
(x);
15
return
value;
16
}
17
catch
(
const
exception
&
e
)
18
{
19
return
0;
20
}
21
}
22
23
int
parse_int
(
const
string
& x)
24
{
25
try
26
{
27
int
value =
stoi
(x);
28
return
value;
29
}
30
catch
(
const
exception
&
e
)
31
{
32
return
0;
33
}
34
}
35
36
string
display_float
(
const
float
& value,
int
number_of_decimals
)
37
{
38
ostringstream
output
;
39
if
(
number_of_decimals
> 0)
40
{
41
output
<< (std::trunc(value*std::pow(10,
number_of_decimals
)) / std::pow(10,
number_of_decimals
));
42
}
43
else
44
{
45
output
<< value;
46
}
47
if
(
output
.str() ==
"-0"
)
48
{
49
return
"0"
;
50
}
51
else
52
{
53
return
output
.str();
54
}
55
}
56
57
string
display_double
(
const
double
& value,
int
number_of_decimals
)
58
{
59
ostringstream
output
;
60
if
(
number_of_decimals
> 0)
61
{
62
output
<< (value*std::pow(10,
number_of_decimals
) / std::pow(10,
number_of_decimals
));
63
}
64
else
65
{
66
output
<< value;
67
}
68
if
(
output
.str() ==
"-0"
)
69
{
70
return
"0"
;
71
}
72
else
73
{
74
return
output
.str();
75
}
76
}
77
78
string
display_long_double
(
const
long
double
& value,
int
number_of_decimals
)
79
{
80
ostringstream
output
;
81
if
(
number_of_decimals
> 0)
82
{
83
output
<< (std::trunc(value*std::pow(10.0l,
number_of_decimals
)) / std::pow(10.0l,
number_of_decimals
));
84
}
85
else
86
{
87
output
<< value;
88
}
89
if
(
output
.str() ==
"-0"
)
90
{
91
return
"0"
;
92
}
93
else
94
{
95
return
output
.str();
96
}
97
}
98
}
scifir::coordinates_1d
Definition
coordinates_1d.hpp:18
scifir
The namespace scifir contains all scifir-units, excepting the string literals, which are outside.
Definition
address.cpp:6
scifir::display_float
string display_float(const float &value, int number_of_decimals)
Definition
types.cpp:36
scifir::display_double
string display_double(const double &value, int number_of_decimals)
Definition
types.cpp:57
scifir::parse_float
float parse_float(const string &x)
Definition
types.cpp:10
scifir::display_long_double
string display_long_double(const long double &value, int number_of_decimals)
Definition
types.cpp:78
scifir::parse_int
int parse_int(const string &x)
Definition
types.cpp:23
types.hpp
Generated by
1.9.8