VTK  9.4.20241227
vtkNumberToString.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
49#ifndef vtkNumberToString_h
50#define vtkNumberToString_h
51
52#include "vtkCommonCoreModule.h" // For export macro
53#include "vtkTypeTraits.h"
54
55#include <ostream>
56#include <string>
57
58VTK_ABI_NAMESPACE_BEGIN
59class VTKCOMMONCORE_EXPORT vtkNumberToString
60{
61public:
63
72 void SetLowExponent(int lowExponent);
75
77
85 void SetHighExponent(int highExponent);
88
90 {
93 Fixed
94 };
95
97
107 void SetNotation(int notation);
110
112
120 void SetPrecision(int precision);
123
125
130 std::string Convert(double val);
131 std::string Convert(float val);
132 template <typename T>
133 std::string Convert(const T& val)
134 {
135 return std::to_string(val);
136 }
138
140 {
141 double Value;
142 TagDouble(const double& value)
143 : Value(value)
144 {
145 }
146 };
147
148 struct TagFloat
149 {
150 float Value;
151 TagFloat(const float& value)
152 : Value(value)
153 {
154 }
155 };
156
157 template <typename T>
158 const T& operator()(const T& val) const
159 {
160 return val;
161 }
162
163private:
164 int LowExponent = -6;
165 int HighExponent = 20;
166 int Notation = Mixed;
167 int Precision = 2;
168};
169
170VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& stream, const vtkNumberToString::TagDouble& tag);
171VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& stream, const vtkNumberToString::TagFloat& tag);
172
173VTK_ABI_NAMESPACE_END
174#endif
175// VTK-HeaderTest-Exclude: vtkNumberToString.h
Convert floating and fixed point numbers to strings.
const T & operator()(const T &val) const
int GetHighExponent()
Set/Get the HighExponent for string conversion.
void SetNotation(int notation)
Set/Get the notation used for string conversion.
int GetLowExponent()
Set/Get the LowExponent for string conversion.
int GetNotation()
Set/Get the notation used for string conversion.
void SetLowExponent(int lowExponent)
Set/Get the LowExponent for string conversion.
std::string Convert(float val)
Convert a number to an accurate string representation of that number.
void SetHighExponent(int highExponent)
Set/Get the HighExponent for string conversion.
std::string Convert(const T &val)
Convert a number to an accurate string representation of that number.
void SetPrecision(int precision)
Set/Get the floating-point precision used for string conversion.
std::string Convert(double val)
Convert a number to an accurate string representation of that number.
int GetPrecision()
Set/Get the floating-point precision used for string conversion.
VTKCOMMONCORE_EXPORT ostream & operator<<(ostream &stream, const vtkNumberToString::TagDouble &tag)