]> Shamusworld >> Repos - architektonas/blob - src/base/rs_utility.cpp
51790c901d135e2bf95193ac305b578843359e42
[architektonas] / src / base / rs_utility.cpp
1 // rs_utility.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // (C) 2010 Underground Software
7 //
8 // JLH = James L. Hammons <jlhamm@acm.org>
9 //
10 // Who  When        What
11 // ---  ----------  -----------------------------------------------------------
12 // JLH  05/05/2010  Moved implementation from header to this file. :-)
13 //
14
15 #include "rs_utility.h"
16
17 /**
18  * Converts a double to a string cutting away unnecessary 0's.
19  * e.g. 2.70000  -> 2.7
20  */
21 QString RS_Utility::doubleToString(double value, int precision/*= 6*/)
22 {
23         QString ret;
24
25         ret.setNum(value, 'f', precision);
26
27         if (ret.contains('.'))
28         {
29                 // remove trailing zeros:
30                 while (ret.at(ret.length() - 1) == '0')
31                 {
32                         ret.truncate(ret.length() - 1);
33                 }
34
35                 // remove trailing .
36                 if (ret.at(ret.length() - 1) == '.')
37                 {
38                         ret.truncate(ret.length() - 1);
39                 }
40         }
41
42         return ret;
43 }