VTK  9.2.20230603
Classes | Public Member Functions | List of all members
vtkNumberToString Class Reference

Convert floating and fixed point numbers to strings. More...

#include <vtkNumberToString.h>

Classes

struct  TagDouble
 
struct  TagFloat
 

Public Member Functions

template<typename T >
const T & operator() (const T &val) const
 
TagDouble operator() (const double &val) const
 
TagFloat operator() (const float &val) const
 
void SetLowExponent (int lowExponent)
 Set/Get the LowExponent for string conversion. More...
 
int GetLowExponent ()
 Set/Get the LowExponent for string conversion. More...
 
void SetHighExponent (int highExponent)
 Set/Get the HighExponent for string conversion. More...
 
int GetHighExponent ()
 Set/Get the HighExponent for string conversion. More...
 
std::string Convert (double val)
 Convert a number to an accurate string representation of that number. More...
 
std::string Convert (float val)
 Set/Get the HighExponent for string conversion. More...
 
template<typename T >
std::string Convert (const T &val)
 Set/Get the HighExponent for string conversion. More...
 

Detailed Description

Convert floating and fixed point numbers to strings.

This class uses the double-conversion library to convert float and double numbers to std::string without numerical precision errors. It is possible to specify the low and high exponent where the string representation will switch to scientific notation instead of fixed point notation.

For other types, this class rely on std::to_string.

Typical use:

float a = 1.0f/3.0f;
std::cout << converter.Convert(a) << std::endl;
Convert floating and fixed point numbers to strings.
std::string Convert(double val)
Convert a number to an accurate string representation of that number.
double a = 1e7*vtkMath::PI();
converter.SetLowExponent(-6);
converter.SetHighExponent(6);
std::cout << converter.Convert(a) << std::endl;
void SetLowExponent(int lowExponent)
Set/Get the LowExponent for string conversion.
void SetHighExponent(int highExponent)
Set/Get the HighExponent for string conversion.
Tests:
vtkNumberToString (Tests)

Definition at line 59 of file vtkNumberToString.h.

Member Function Documentation

◆ SetLowExponent()

void vtkNumberToString::SetLowExponent ( int  lowExponent)

Set/Get the LowExponent for string conversion.

It correspond to the closest to zero exponent value that will use fixed point notation in the returned string instead of a scientific notation. eg: LowExponent = 6, 1e-6 -> "0.000001" LowExponent = 5, 1e-6 -> "1e-6"

◆ GetLowExponent()

int vtkNumberToString::GetLowExponent ( )

Set/Get the LowExponent for string conversion.

It correspond to the closest to zero exponent value that will use fixed point notation in the returned string instead of a scientific notation. eg: LowExponent = 6, 1e-6 -> "0.000001" LowExponent = 5, 1e-6 -> "1e-6"

◆ SetHighExponent()

void vtkNumberToString::SetHighExponent ( int  highExponent)

Set/Get the HighExponent for string conversion.

HighExponent correspond to the highest exponent value that will use fixed point notation in the returned string instead of a scientific notation. HighExponent = 6, 1e6 -> "1000000" HighExponent = 5, 1e6 -> "1e6"

◆ GetHighExponent()

int vtkNumberToString::GetHighExponent ( )

Set/Get the HighExponent for string conversion.

HighExponent correspond to the highest exponent value that will use fixed point notation in the returned string instead of a scientific notation. HighExponent = 6, 1e6 -> "1000000" HighExponent = 5, 1e6 -> "1e6"

◆ Convert() [1/3]

std::string vtkNumberToString::Convert ( double  val)

Convert a number to an accurate string representation of that number.

A templated generic implementation is provided, which rely on std::to_string for types other than double or float.

◆ Convert() [2/3]

std::string vtkNumberToString::Convert ( float  val)

Set/Get the HighExponent for string conversion.

HighExponent correspond to the highest exponent value that will use fixed point notation in the returned string instead of a scientific notation. HighExponent = 6, 1e6 -> "1000000" HighExponent = 5, 1e6 -> "1e6"

◆ Convert() [3/3]

template<typename T >
std::string vtkNumberToString::Convert ( const T &  val)
inline

Set/Get the HighExponent for string conversion.

HighExponent correspond to the highest exponent value that will use fixed point notation in the returned string instead of a scientific notation. HighExponent = 6, 1e6 -> "1000000" HighExponent = 5, 1e6 -> "1e6"

Definition at line 95 of file vtkNumberToString.h.

◆ operator()() [1/3]

template<typename T >
const T& vtkNumberToString::operator() ( const T &  val) const
inline

Definition at line 120 of file vtkNumberToString.h.

◆ operator()() [2/3]

TagDouble vtkNumberToString::operator() ( const double &  val) const
inline

Definition at line 125 of file vtkNumberToString.h.

◆ operator()() [3/3]

TagFloat vtkNumberToString::operator() ( const float &  val) const
inline

Definition at line 127 of file vtkNumberToString.h.


The documentation for this class was generated from the following file: