Article number: KB000012
Related blocks:
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 is 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.
All transformations are executed 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
All transformations are executed taking into account regional settings and operating system.
Method | Syntax | Description |
FormatDouble(Value: Double): String; | Converts a real number into a string. | |
FormatDoublePrec (Value:Double; Precision: Integer): String; |
Converts a real number into a string with specified accuracy. | |
ParseDouble(Value: String): Double; | Returns a real number converted from a string. | |
TryParseDouble (Value: String; Result: Double): Boolean; |
Returns True if transformation of a real number from a string is successful. | |
FormatDateTime(Value: DateTime): String; | Converts a date (with time) to a string. | |
FormatDate(Value: DateTime): String; | Converts a date (without time) to a string. | |
FormatDateEx (Value: DateTime; Format: String): String; |
Converts a date to a string considering the specified format. | |
FormatShortDate(Value: DateTime): String; | Converts a date to a string (short format, for example, 06.06.2006). | |
FormatLongDate(Value: DateTime): String; | Converts a date to a string (full format, for example, 6 June 2006). | |
ParseDateTime(Value: String): DateTime; | Returns a date (with time) converted from a string. | |
ParseDate(Value: String): DateTime; | Returns a date (without time) converted from a string. |
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 the example the console window displays results of converting real number and date considering and not considering regional settings and operating system.
Results of executing the example in Russian Windows:
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 the 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: