|
| 1 | +// Copyright (C) 2005-2009 by Jukka Korpela |
| 2 | +// Copyright (C) 2009-2013 by David Hoerl |
| 3 | +// Copyright (C) 2013 by Martin Moene |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in |
| 13 | +// all copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +// THE SOFTWARE. |
| 22 | + |
| 23 | +#ifndef ENG_FORMAT_H_INCLUDED |
| 24 | +#define ENG_FORMAT_H_INCLUDED |
| 25 | + |
| 26 | +#include <string> |
| 27 | + |
| 28 | +/** |
| 29 | + * convert a double to the specified number of digits in SI (prefix) or |
| 30 | + * exponential notation, optionally followed by a unit. |
| 31 | + */ |
| 32 | +std::string |
| 33 | +to_engineering_string( double value, int digits, bool exponential, std::string unit = "", std::string separator = " " ); |
| 34 | + |
| 35 | +/** |
| 36 | + * convert the output of to_engineering_string() into a double. |
| 37 | + */ |
| 38 | +double |
| 39 | +from_engineering_string( std::string text ); |
| 40 | + |
| 41 | +/** |
| 42 | + * step a value by the smallest possible increment. |
| 43 | + */ |
| 44 | +std::string |
| 45 | +step_engineering_string( std::string text, int digits, bool exponential, bool increment ); |
| 46 | + |
| 47 | +// |
| 48 | +// Extended interface: |
| 49 | +// |
| 50 | + |
| 51 | +/** |
| 52 | + * \var eng_prefixed |
| 53 | + * \brief select SI (prefix) presentation: to_engineering_string(), step_engineering_string(). |
| 54 | + */ |
| 55 | + |
| 56 | +/** |
| 57 | + * \var eng_exponential |
| 58 | + * \brief select exponential presentation: to_engineering_string(), step_engineering_string(). |
| 59 | + */ |
| 60 | + |
| 61 | +extern struct eng_prefixed_t {} eng_prefixed; |
| 62 | +extern struct eng_exponential_t {} eng_exponential; |
| 63 | + |
| 64 | +/** |
| 65 | + * \var eng_increment |
| 66 | + * \brief let step_engineering_string() make a postive step. |
| 67 | + */ |
| 68 | + |
| 69 | +/** |
| 70 | + * \var eng_decrement |
| 71 | + * \brief let step_engineering_string() make a negative step. |
| 72 | + */ |
| 73 | + |
| 74 | +constexpr bool eng_increment = true; |
| 75 | +constexpr bool eng_decrement = false; |
| 76 | + |
| 77 | +/** |
| 78 | + * convert a double to the specified number of digits in SI (prefix) notation, |
| 79 | + * optionally followed by a unit. |
| 80 | + */ |
| 81 | +inline std::string |
| 82 | +to_engineering_string( double value, int digits, eng_prefixed_t, std::string unit = "", std::string separator = " " ) |
| 83 | +{ |
| 84 | + return to_engineering_string( value, digits, false, unit, separator ); |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * convert a double to the specified number of digits in exponential notation, |
| 89 | + * optionally followed by a unit. |
| 90 | + */ |
| 91 | +inline std::string |
| 92 | +to_engineering_string( double value, int digits, eng_exponential_t, std::string unit = "", std::string separator = " " ) |
| 93 | +{ |
| 94 | + return to_engineering_string( value, digits, true, unit, separator ); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * step a value by the smallest possible increment, using SI notation. |
| 99 | + */ |
| 100 | +inline std::string |
| 101 | +step_engineering_string( std::string text, int digits, eng_prefixed_t, bool increment ) |
| 102 | +{ |
| 103 | + return step_engineering_string( text, digits, false, increment ); |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * step a value by the smallest possible increment, using exponential notation. |
| 108 | + */ |
| 109 | +inline std::string |
| 110 | +step_engineering_string( std::string text, int digits, eng_exponential_t, bool increment ) |
| 111 | +{ |
| 112 | + return step_engineering_string( text, digits, true, increment ); |
| 113 | +} |
| 114 | + |
| 115 | +#endif // ENG_FORMAT_H_INCLUDED |
0 commit comments