VTK  9.3.20240418
Classes | Public Types | 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 Types

enum  Notation { Mixed , Scientific , Fixed }
 

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...
 
void SetNotation (int notation)
 Set/Get the notation used for string conversion. More...
 
int GetNotation ()
 Set/Get the notation used for string conversion. More...
 
void SetPrecision (int precision)
 Set/Get the floating-point precision used for string conversion. More...
 
int GetPrecision ()
 Set/Get the floating-point precision used 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)
 Convert a number to an accurate string representation of that number. More...
 
template<typename T >
std::string Convert (const T &val)
 Convert a number to an accurate string representation of that number. 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. You can use specify the output format using SetNotation to either Mixed, Scientific or Fixed. In Mixed mode (default), it is possible to specify the low and high exponent where the string representation will switch to scientific notation instead of fixed point notation.

Unless specified using SetPrecision, the formatted value will not have trailing zeroes.

For types other than float and double, this class relies 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.
double a = 4.2;
converter.SetPrecision(4);
std::cout << converter.Convert(a) << std::endl;
void SetNotation(int notation)
Set/Get the notation used for string conversion.
void SetPrecision(int precision)
Set/Get the floating-point precision used for string conversion.
Tests:
vtkNumberToString (Tests)

Definition at line 60 of file vtkNumberToString.h.

Member Enumeration Documentation

◆ Notation

Enumerator
Mixed 
Scientific 
Fixed 

Definition at line 90 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. Only used when Notation value is Mixed (default). 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. Only used when Notation value is Mixed (default). 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. Only used when Notation value is Mixed (default). 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. Only used when Notation value is Mixed (default). HighExponent = 6, 1e6 -> "1000000" HighExponent = 5, 1e6 -> "1e6"

◆ SetNotation()

void vtkNumberToString::SetNotation ( int  notation)

Set/Get the notation used for string conversion.

Mixed (0) will choose between fixed-point and scientific notation depending on HighExponent and LowExponent. Scientific (1) will always use scientific notation Fixed (2) will always use fixed-point notation. Note that Fixed can't be used for values that have more than 60 digits either before or after the decimal point. Default is 0 (Mixed)

◆ GetNotation()

int vtkNumberToString::GetNotation ( )

Set/Get the notation used for string conversion.

Mixed (0) will choose between fixed-point and scientific notation depending on HighExponent and LowExponent. Scientific (1) will always use scientific notation Fixed (2) will always use fixed-point notation. Note that Fixed can't be used for values that have more than 60 digits either before or after the decimal point. Default is 0 (Mixed)

◆ SetPrecision()

void vtkNumberToString::SetPrecision ( int  precision)

Set/Get the floating-point precision used for string conversion.

The precision specifies the number of decimal places to display for Scientific and Fixed-point notations. In Mixed mode, this parameter is not used, and the string will display as many decimal places as needed in order not to have any trailing zeroes and keep full precision. Default is 2.

◆ GetPrecision()

int vtkNumberToString::GetPrecision ( )

Set/Get the floating-point precision used for string conversion.

The precision specifies the number of decimal places to display for Scientific and Fixed-point notations. In Mixed mode, this parameter is not used, and the string will display as many decimal places as needed in order not to have any trailing zeroes and keep full precision. Default is 2.

◆ 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)

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() [3/3]

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

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.

Definition at line 134 of file vtkNumberToString.h.

◆ operator()() [1/3]

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

Definition at line 159 of file vtkNumberToString.h.

◆ operator()() [2/3]

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

Definition at line 164 of file vtkNumberToString.h.

◆ operator()() [3/3]

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

Definition at line 166 of file vtkNumberToString.h.


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