In this article:

General Information

Description

ToString and Parse Methods

Methods of the CultureInfo Class Used to Convert Real Numbers and Dates

Example

Features of Transformation of Real Numbers and Dates into Strings and Back

Article number: KB000012

General Information

Related blocks:

Description

There are two methods of converting real numbers or dates into strings and back: using the ToString and Parse methods and methods of the CultureInfo static class.

The ToString and Parse methods are used to convert numbers or dates without taking into account regional settings and operating system. The ToString method returns a string that can be saved to a database. On another computer (with another operating system or regional settings) this database string can be converted to original number by calling the Parse method. These data are correct.

Methods of the CultureInfo static class are used to convert numbers and dates taking into account regional settings and operating system of a user, that is to display data correctly.

ToString and Parse Methods

All transformations are performed without taking into account regional settings and operating system.

Method Syntax Description
ToString ToString; Converts a value to a string.
Parse Parse(Value: String); Returns a value converted from a string to a system type.

See also:

Double.ToStringDouble.ToString | Double.ParseDouble.Parse | DateTime.ToStringDateTime.ToString | DateTime.ParseDateTime.Parse

Methods of the CultureInfo Class Used to Convert Real Numbers and Dates

All transformations are performed taking into account regional settings and operating system.

Method Syntax Description

FormatDouble

FormatDouble(Value: Double): String; Converts a real number into a string.

FormatDoublePrec

FormatDoublePrec
(Value:Double; Precision: Integer): String;
Converts a real number into a string with specified accuracy.

ParseDouble

ParseDouble(Value: String): Double; Returns a real number converted from a string.

TryParseDouble

TryParseDouble
(Value: String; Result: Double): Boolean;
Returns True if transformation of a real number from a string is successful.

FormatDateTime

FormatDateTime(Value: DateTime): String; Converts a date (with time) into a string.

FormatDate

FormatDate(Value: DateTime): String; Converts a date (without time) into a string.

FormatDateEx

FormatDateEx
(Value: DateTime; Format: String): String;
Converts a date into a string considering the specified format.

FormatShortDate

FormatShortDate(Value: DateTime): String; Convert a date into a string (short format, for example, 06.06.2006).

FormatLongDate

FormatLongDate(Value: DateTime): String; Converts a date into a string (full format, for example, 6 June 2006).

ParseDateTime

ParseDateTime(Value: String): DateTime; Returns a date (with time) converted from a string.

ParseDate

ParseDate(Value: String): DateTime; Returns a date (without time) converted from a string.

Example

Sub UserProc;
Var
    CF: ICultureInfo;
    s: String;
Begin
    CF := CultureInfo.Current;
    s := CF.FormatDouble(3.14);
    Debug.WriteLine("FormatDouble: " + s);
    s := (3.14).ToString;
    Debug.WriteLine("ToString: " + s);
    s := CF.FormatDateEx(DateTime.Now, "dddd, MM/yyyy");
    Debug.WriteLine("FormatDateEx: " + s);
    s := (DateTime.Now).ToString;
    Debug.WriteLine("ToString: " + s);
End Sub UserProc;

After executing this example the console window displays results of converting real number and date considering and not considering regional parameters and operating system.

Results of executing this example in Russian Windows OS:

Module execution started
FormatDouble: 3,14
ToString: 3.1400000000000001
FormatDateEx: Thursday, 06/2010
ToString: 10.06.2010 16:10:50
Module execution finished

Results of executing this example in English Windows:

Module execution started
FormatDouble: 3.14
ToString: 3.1400000000000001
FormatDateEx: Thursday, 06/2010
ToString: 10.06.2010 16:10:58
Module execution finished

See also:

Developers Knowledge Base