Type Conversions
Where string is a signed decimal number:
| string to integer | string to long integer | string to floating point | integer to string | floating point to string | |
|---|---|---|---|---|---|
| ALGOL 68 with general, and then specific formats | With prior declarations and association of: string buf := "12345678.9012e34 "; file proxy; associate(proxy, buf); | ||||
| get(proxy, ivar); | get(proxy, livar); | get(proxy, rvar); | put(proxy, ival); | put(proxy, rval); | |
| getf(proxy, ($g$, ivar)); or
getf(proxy, ($dddd$, ivar)); |
getf(proxy, ($g$, livar)); or
getf(proxy, ($8d$, livar)); |
getf(proxy, ($g$, rvar)); or
getf(proxy, ($8d.4dE2d$, rvar)); |
putf(proxy, ($g$, ival)); or
putf(proxy, ($4d$, ival)); |
putf(proxy, ($g(width, places, exp)$, rval)); or
putf(proxy, ($8d.4dE2d$, rval)); etc. |
|
| C (C99) | integer = atoi(string); | long = atol(string); | float = atof(string); | sprintf(string, "%i", integer); | sprintf(string, "%f", float); |
| Objective-C | integer = ; | long = ; | float = ; | string = ; | string = ; |
| C++ (STL) | «std::»istringstream(string) >> number; | «std::»ostringstream o; o << number; string = o.str; | |||
| C++11 | integer = «std::»stoi(string); | long = «std::»stol(string); | float = «std::»stof(string);
double = «std::»stod(string); |
string = «std::»to_string(number); | |
| C# | integer = int.Parse(string); | long = long.Parse(string); | float = float.Parse(string); or double = double.Parse(string); |
string = number.ToString; | |
| D | integer = std.conv.to!int(string) | long = std.conv.to!long(string) | float = std.conv.to!float(string) or double = std.conv.to!double(string) |
string = std.conv.to!string(number) | |
| Java | integer = Integer.parseInt(string); | long = Long.parseLong(string); | float = Float.parseFloat(string); or double = Double.parseDouble(string); |
string = Integer.toString(integer); | string = Float.toString(float); or string = Double.toString(double); |
| JavaScript | integer = parseInt(string); | float = parseFloat(string); or float = new Number (string) or float = Number (string) or float = string*1; |
string = number.toString ; or string = new String (number); or string = String (number); or string = number+""; |
||
| Go | integer, error = strconv.Atoi(string) or integer, error = strconv.ParseInt(string, 10, 0) |
long, error = strconv.ParseInt(string, 10, 64) | float, error = strconv.ParseFloat(string, 64) | string = strconv.Itoa(integer) or string = strconv.FormatInt(integer, 10) or string = fmt.Sprint(integer) |
string = strconv.FormatFloat(float) or string = fmt.Sprint(float) |
| Common Lisp | (setf integer (parse-integer string)) | (setf float (read-from-string string)) | (setf string (princ-to-string number)) | ||
| Scheme | (define number (string->number string)) | (define string (number->string number)) | |||
| ISLISP | (setf integer (convert string |
(setf float (convert string |
(setf string (convert number |
||
| Pascal | integer := StrToInt(string); | float := StrToFloat(string); | string := IntToStr(integer); | string := FloatToStr(float); | |
| Visual Basic | integer = CInt(string) | long = CLng(string) | float = CSng(string) or double = CDbl(string) |
string = CStr(number) | |
| Visual Basic .NET | |||||
| Python | integer = int(string) | long = long(string) | float = float(string) | string = str(number) | |
| S-Lang | integer = atoi(string); | long = atol(string); | float = atof(string); | string = string(number); | |
| Fortran | READ(string,format) number | WRITE(string,format) number | |||
| PHP | integer = intval(string); or integer = (int)string; |
float = floatval(string); or float = (float)string; |
string = "number"; or string = strval(number); or string = (string)number; |
||
| Perl | number = 0 + string; | string = "number"; | |||
| Perl 6 | number = +string; | string = ~number; | |||
| Ruby | integer = string.to_i or integer = Integer(string) |
float = string.to_f or float = Float(string) |
string = number.to_s | ||
| Windows PowerShell | integer = string | long = string | float = string | string = number; or string = "number"; or string = (number).ToString |
|
| OCaml | let integer = int_of_string string | let float = float_of_string string | let string = string_of_int integer | let string = string_of_float float | |
| F# | let integer = int string | let integer = int64 string | let float = float string | let string = string number | |
| Standard ML | val integer = Int.fromString string | val float = Real.fromString string | val string = Int.toString integer | val string = Real.toString float | |
| Haskell (GHC) | number = read string | string = show number | |||
^a JavaScript only uses floating point numbers so there are some technicalities.
^b Perl doesn't have separate types. Strings and numbers are interchangeable.
Read more about this topic: Comparison Of Programming Languages (basic Instructions)
Famous quotes containing the word type:
“There is a limit to the application of democratic methods. You can inquire of all the passengers as to what type of car they like to ride in, but it is impossible to question them as to whether to apply the brakes when the train is at full speed and accident threatens.”
—Leon Trotsky (18791940)