VTK  9.7.1
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 "vtkStringFormatter.h" // For vtk::to_string/format
54 
55 #include <ostream>
56 #include <string>
57 
58 VTK_ABI_NAMESPACE_BEGIN
59 class VTKCOMMONCORE_EXPORT vtkNumberToString
60 {
61 public:
63 
72  void SetLowExponent(int lowExponent);
73  int GetLowExponent();
75 
77 
85  void SetHighExponent(int highExponent);
86  int GetHighExponent();
88 
89  enum Notation
90  {
93  Fixed
94  };
95 
97 
107  void SetNotation(int notation);
108  int GetNotation();
110 
112 
120  void SetPrecision(int precision);
121  int GetPrecision();
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 vtk::to_string(val);
136  }
138 
139  struct TagDouble
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  T operator()(const T& val) const
159  {
160  return val;
161  }
162 
163 private:
164  int LowExponent = -6;
165  int HighExponent = 20;
166  int Notation = Mixed;
167  int Precision = 2;
168 };
169 
170 VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& stream, const vtkNumberToString::TagDouble& tag);
171 VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& stream, const vtkNumberToString::TagFloat& tag);
172 
173 VTK_ABI_NAMESPACE_END
174 #endif
175 // VTK-HeaderTest-Exclude: vtkNumberToString.h
std::string VTKCOMMONCORE_EXPORT to_string(vtkArrayComponents enumerant)
Given an enumerant, return a human-presentable string with its value.
TagDouble(const double &value)
T operator()(const T &val) const
std::string Convert(const T &val)
Convert a number to an accurate string representation of that number.
TagFloat(const float &value)
Convert floating and fixed point numbers to strings.
VTKACCELERATORSVTKMCORE_EXPORT vtkDataArray * Convert(const viskores::cont::Field &input)
VTKCOMMONCORE_EXPORT ostream & operator<<(ostream &stream, const vtkNumberToString::TagDouble &tag)
Optimized C++ utilities for formatting values to strings and files.