1 #ifndef SEEN_DECIMAL_ROUND_H
2 #define SEEN_DECIMAL_ROUND_H
4 #include <cmath>
6 #include "round.h"
9 namespace Inkscape {
11 /** Returns x rounded to the nearest \a nplaces decimal places.
13 Implemented in terms of Inkscape::round, i.e. we make no guarantees as to what happens if x is
14 half way between two rounded numbers. Add a note to the documentation if you care which
15 candidate is chosen for such case (round towards -infinity, +infinity, 0, or "even").
17 Note: nplaces is the number of decimal places without using scientific (e) notation, not the
18 number of significant figures. This function may not be suitable for values of x whose
19 magnitude is so far from 1 that one would want to use scientific (e) notation.
21 The current implementation happens to allow nplaces to be negative: e.g. nplaces=-2 means
22 rounding to a multiple of 100. May or may not be useful.
23 **/
24 inline double
25 decimal_round(double const x, int const nplaces)
26 {
27 double const multiplier = std::pow(10.0, nplaces);
28 return Inkscape::round( x * multiplier ) / multiplier;
29 }
31 }
33 #endif /* !SEEN_DECIMAL_ROUND_H */
35 /*
36 Local Variables:
37 mode:c++
38 c-file-style:"stroustrup"
39 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
40 indent-tabs-mode:nil
41 fill-column:99
42 End:
43 */
44 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :