Changeset 721 for code/branches/FICN/src/misc/String2Number.h
- Timestamp:
- Dec 29, 2007, 3:14:39 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/FICN/src/misc/String2Number.h
r714 r721 49 49 }; 50 50 51 52 /**53 @brief Converts a number of any type to a string.54 @param variable The variable, containing the number.55 56 @example57 float f = 3.14;58 std::string output = number2String(f);59 */60 template <typename T>61 const std::string number2String(T variable)62 {63 std::ostringstream oss;64 if (oss << variable)65 return std::string(oss.str());66 67 return std::string("");68 }69 70 /**71 @brief Converts a number of any type to a string and assigns a defaultvalue if the conversion fails.72 @param variable The variable, containing the number.73 @param fallbackString The assigned string if the conversion fails.74 75 @example76 float f = 3.14;77 std::string output = number2String(f, "0.000000");78 */79 template <typename T>80 const std::string number2String(T variable, const std::string& fallbackString)81 {82 std::ostringstream oss;83 if (oss << variable)84 return std::string(oss.str());85 86 return fallbackString;87 }88 89 /**90 @brief Converts a string to a number of any type.91 @param variable The variable wherein the value gets written92 @return True if the conversion was successful93 94 @example95 float f;96 bool success = string2Number(f, "3.14");97 */98 template <typename T>99 bool string2Number(T& variable, const std::string& valueString)100 {101 std::istringstream iss(valueString);102 if (iss >> variable)103 return true;104 105 return false;106 }107 108 /**109 @brief Converts a string to a number of any type.110 @param variable The variable wherein the value gets written111 @param fallbackValue The assigned value if the conversion fails112 @return True if the conversion was successful113 114 @example115 float f;116 bool success = string2Number(f, "3.14", 0.000000);117 */118 template <typename T>119 bool string2Number(T& variable, const std::string& valueString, T fallbackValue)120 {121 std::istringstream iss(valueString);122 if (iss >> variable)123 return true;124 125 variable = fallbackValue;126 return false;127 }128 129 51 #endif /* _String2Number_H__ */
Note: See TracChangeset
for help on using the changeset viewer.