Current Version: 1.0.33
Project Name: csspp
csspp.cpp
Go to the documentation of this file.
1// Copyright (c) 2015-2025 Made to Order Software Corp. All Rights Reserved
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation; either version 2 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License along
14// with this program; if not, write to the Free Software Foundation, Inc.,
15// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16
27// self
28//
29#include "csspp/csspp.h"
30
31#include "csspp/exception.h"
32
33
34// C++
35//
36#include <cmath>
37#include <cfloat>
38#include <iomanip>
39#include <sstream>
40#include <iostream>
41
42
43// last include
44//
45#include <snapdev/poison.h>
46
47
48
56namespace csspp
57{
58
59namespace
60{
61
63
64} // no name namespace
65
67{
68 return CSSPP_VERSION;
69}
70
72{
73 return g_precision;
74}
75
76void set_precision(int precision)
77{
78 if(precision < 0 || precision > 10)
79 {
80 throw csspp_exception_overflow("precision must be between 0 and 10, " + std::to_string(precision) + " is out of bounds.");
81 }
82
83 g_precision = precision;
84}
85
86std::string decimal_number_to_string(decimal_number_t d, bool remove_leading_zero)
87{
88 // the std::setprecision() is a total number of digits when we
89 // want a specific number of digits after the decimal point so
90 // we use the following algorithm for it:
91 std::stringstream ss;
92
93 // use the maximum precision so we do not get any surprises
94 // see the following for the "3 + DBL_MANT_DIG - DBL_MIN_EXP":
95 // http://stackoverflow.com/questions/1701055/what-is-the-maximum-length-in-chars-needed-to-represent-any-double-value
96 ss << std::setprecision(3 + DBL_MANT_DIG - DBL_MIN_EXP);
97
98 // make sure to round the value up first
99 if(d >= 0.0)
100 {
101 ss << d + 0.5 / pow(10.0, g_precision);
102 }
103 else
104 {
105 ss << d - 0.5 / pow(10.0, g_precision);
106 }
107
108 std::string out(ss.str());
109
110 // check wether the number of digits after the decimal point is too large
111 std::string::size_type end(out.find('.'));
112 if(end != std::string::npos
113 && out.length() > end + g_precision + 1)
114 {
115 // remove anything extra
116 out = out.substr(0, end + g_precision + 1);
117 }
118 if(out.find('.') != std::string::npos
119 && end != std::string::npos)
120 {
121 while(out.back() == '0')
122 {
123 out.erase(out.end() - 1);
124 }
125 if(out.back() == '.')
126 {
127 out.erase(out.end() - 1);
128 }
129 }
130
131 // remove the leading zero when possible
132 if(remove_leading_zero)
133 {
134 if(out.length() >= 3
135 && out[0] == '0'
136 && out[1] == '.')
137 {
138 out.erase(out.begin());
139 // .33 is valid and equivalent to 0.33
140 }
141 else if(out.length() >= 4
142 && out[0] == '-'
143 && out[1] == '0'
144 && out[2] == '.')
145 {
146 out.erase(out.begin() + 1);
147 // -.33 is valid and equivalent to -0.33
148 }
149 }
150
151 if(out == "-0")
152 {
153 // this happens with really small numbers because we lose
154 // the precision and thus end up with zero even if the number
155 // was not really zero
156 return "0";
157 }
158
159 return out;
160}
161
162} // namespace csspp
163
164// Local Variables:
165// mode: cpp
166// indent-tabs-mode: nil
167// c-basic-offset: 4
168// tab-width: 4
169// End:
170
171// vim: ts=4 sw=4 et
#define CSSPP_VERSION
Definition csspp.h:53
The namespace of all the classes in the CSS Preprocessor.
Definition csspp.h:48
int get_precision()
Definition csspp.cpp:71
void set_precision(int precision)
Definition csspp.cpp:76
std::string decimal_number_to_string(decimal_number_t d, bool remove_leading_zero)
Definition csspp.cpp:86
char const * csspp_library_version()
Definition csspp.cpp:66
double decimal_number_t
Definition csspp.h:59

Documentation of CSS Preprocessor.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.