ToString(string, FormatStyle, int32)

Method

Parses and converts an input string into a new string, formatted according to the rules of the specific data type.

Product: 

Class: 

Warning

uCalc API Preview Release Notice:The uCalc engine has successfully transitioned to modern cross-platform environments.The next phase envolves some structural changes, performance optimizations, and API refinements.The API is subject to breaking changes prior to the stable release. Please evaluate the preview version thoroughly before production use.

Syntax

ToString(string, FormatStyle, int32)

Parameters

valueString
string
The input string to be parsed and converted.
formatStyle
FormatStyle
(Default = FormatStyle::Default)
The formatting style to apply to numeric results (e.g., General, Scientific, Fixed, Hex).
precision
int32
(Default = -1)
The number of digits of precision to include for numeric results. If omitted or set to `-1`, default precision is used.

Return

string

The resulting string after parsing the input and reformatting it based on the data type's rules.

Remarks

Syntax (Overload - Custom Format Expression): ToString(string valueString, string formatString)

Parameters (Overload):

  • valueString - string - The input string to be parsed and converted.
  • formatString - string - A formatting expression in the form of "Result = [expression]" that manipulates the output before returning it.

Return:string - The resulting string after parsing the input and reformatting it.

The ToString method is a powerful, type-aware parsing utility. Unlike a typical ToString function that serializes an object's current state, this method takes an external string and interprets it according to the rules of the DataType object it is called on.

This is particularly useful for data normalization and validation, as it can convert a single string representation into the canonical format for a given type.

For example, when called on an unsigned 8-bit integer type, it correctly handles overflow wrapping:

using uCalcSoftware;

var uc = new uCalc();

// The Int8u type interprets -1 as its max value (255)
var dt = uc.DataTypeOf(BuiltInType.Integer_8u);
Console.WriteLine(dt.ToString("-1")); // Output: 255

🎨 Output Formatting

Like other string-returning methods, ToString provides two primary approaches for controlling how the converted result is formatted:

  1. FormatStyle Enum & Precision: Pass a FormatStyle enum (such as FormatStyle.Scientific or FormatStyle.Hex) and an optional precision integer to apply standardized numeric formatting. FormatStyle.Default will apply any global rules defined via uCalc.Format().
  2. Custom Format Expressions: Pass a formatting expression (e.g., "Result = Format('{:6}', double(Result))") to manipulate the output dynamically.

See also: ToString(pointer), the overload which converts a value directly from a memory address.

ToString vs. EvalStr

It is crucial to distinguish this method from EvalStr:

  • DataType.ToString(string): Parses a single literal value. It does not evaluate expressions. For example, uc.DataTypeOf("Int").ToString("1+1") would likely fail to parse "1+1" as a valid integer and return "0".
  • uc.EvalStr(string): Parses and evaluates a full expression. For example, uc.EvalStr("1+1") returns "2".

⚖️ Comparative Analysis

In languages like C#, parsing is a static operation tied to a specific type (e.g., int.Parse(), bool.Parse()). This requires compile-time knowledge of the target type.

uCalc's approach is dynamic. You can hold a DataType object in a variable and call ToString on it without knowing at compile time which specific type it represents. This enables the creation of generic, powerful data validation and conversion routines that can be configured at runtime.

See also: ToString(pointer), the overload which converts a value from a memory address.

Examples

Demonstrates how different data types interpret the same input string, '-1', according to their specific rules, with ToString

ID: 92

				
					using uCalcSoftware;

var uc = new uCalc();
Console.WriteLine(uc.DataTypeOf(BuiltInType.Integer_8).ToString("-1"));
Console.WriteLine(uc.DataTypeOf(BuiltInType.Integer_8u).ToString("-1"));
Console.WriteLine(uc.DataTypeOf(BuiltInType.Integer_16u).ToString("-1"));
Console.WriteLine(uc.DataTypeOf(BuiltInType.Boolean).ToString("-1"));
Console.WriteLine(uc.DataTypeOf(BuiltInType.String).ToString("-1"));
				
			
-1
255
65535
true
-1
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   cout << uc.DataTypeOf(BuiltInType::Integer_8).ToString("-1") << endl;
   cout << uc.DataTypeOf(BuiltInType::Integer_8u).ToString("-1") << endl;
   cout << uc.DataTypeOf(BuiltInType::Integer_16u).ToString("-1") << endl;
   cout << uc.DataTypeOf(BuiltInType::Boolean).ToString("-1") << endl;
   cout << uc.DataTypeOf(BuiltInType::String).ToString("-1") << endl;
}
				
			
-1
255
65535
true
-1
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Console.WriteLine(uc.DataTypeOf(BuiltInType.Integer_8).ToString("-1"))
      Console.WriteLine(uc.DataTypeOf(BuiltInType.Integer_8u).ToString("-1"))
      Console.WriteLine(uc.DataTypeOf(BuiltInType.Integer_16u).ToString("-1"))
      Console.WriteLine(uc.DataTypeOf(BuiltInType.Boolean).ToString("-1"))
      Console.WriteLine(uc.DataTypeOf(BuiltInType.String).ToString("-1"))
   End Sub
End Module
				
			
-1
255
65535
true
-1