Data Type Conversion

Data type conversions can be achieved through either data type conversion functions or function cast($).

DolphinDB supports type conversion functions including string, bool, char, short, int, long, double, date, month, time, second, minute, datetime, timestamp, symbol, nanotime, nanotimestamp, datehour, uuid, ipaddr, int128, blob, complex, point, duration.

Each individual function has 3 usages:

  • Create a new variable with NULL value

  • Convert from a string

  • Convert from other data types

Note:

  • All these functions (except for symbol) accept zero or one parameter. If there is no input parameter, it creates a corresponding scalar object with a default value. If the parameter is a string or string vector, it will convert the string to the target data type accordingly. Other types are converted accordingly if they are semantically compatible.

  • The short, int, and long functions use rounding to convert floating-point numbers to integers, and use truncation to convert strings by discarding the fractional part of that given value.

string

// make a new string with default value ""
$ string()=="";
1

$ string(10);
10

$ typestr string(108.5);
STRING

$ string(now());
2016.03.02T20:55:31.287

bool

$ x=bool();
$ x;
00b
$ typestr x;
BOOL

$ bool(`true);
1
$ bool(`false);
0

$ bool(100.2);
1

$ bool(0);
0

int

$ x=int();
$ x;
00i
$ typestr x;
INT

$ int(`10.9);
10

$ int(2147483647);
2147483647

// maximum value for an INT is 2^31-1=2147483647
$ int(2147483648);
00i

short

$ x=short();
$ x;
00h
$ typestr x;
SHORT

$ short(`12.3);
12

$ short(`120.9c);
120

$ short(32767);
32767

 // maximum value for a SHORT is 2^15-1=32767
$ short(32768);
00h

long

$ x=long();
$ x;
00l
$ typestr x;
LONG

$ long(`10.9);
10

$ long(9223372036854775807l);
9223372036854775807

// maximum value for LONG is 2^63-1=9223372036854775807
$ long(9223372036854775808l);
9223372036854775807

char

$ x=char();
$ x;
00c
$ typestr x;
CHAR

$ a=char(99);
$ a;
'c'
$ typestr a;
CHAR
$ char(a+5);
'h'

double

$ x=double();
$ x;
00F
$ typestr x;
DOUBLE

$ typestr double(`10);
DOUBLE

$ double(`10.9);
10.9

$ double(now());
5.297834e+011

date

$ date();
00d

$ date(`2011.10.12);
2011.10.12

$ date(now());
2016.03.02

datetime

$ datetime(2009.11.10);
2009.11.10T00:00:00
$ typestr datetime(2009.11.10);
DATETIME

$ datetime(now());
2016.03.02T20:51:10

timestamp

$ timestamp(2016.10.12);
2016.10.12T00:00:00.000

$ timestamp(2016.10.12)+1;
2016.10.12T00:00:00.001

$ timestamp(now());
2016.10.13T20:28:45.104

month

 $ month();
 00M

 $ month(`2012.12m);
 2012.12M

 //make a month variable from date
 $ month(2012.12.23);
 2012.12M

//make a month variable from timestamp
 $ month(now());
 2016.03M

second

$ second();
00s

$ second("19:36:12");
19:36:12

$ second(now());
20:50:31

minute

$ minute();
00m

$ minute(now());
20:49m

time

$ time();
00t

$ time("12:32:56.356");
12:32:56.356

$ time(now());
20:49:12.564

symbol

$ x=`AMZN`AAPL`GOOG`FB`SNAP;

$ x;
["AMZN","AAPL","GOOG","FB","SNAP"]

$ typestr x;
STRING VECTOR

$ y=symbol(x);

$ y;
["AMZN","AAPL","GOOG","FB","SNAP"]

$ typestr y;
FAST SYMBOL VECTOR

cast(X, dataTypeName) / $

$ x=3.1;
$ typestr x;
DOUBLE

$ x=cast(x, int);
// can also use cast(x, INT)

$ x;
3
$ typestr x;
INT

$ 19.99$INT;
20

$ syms =`IBM`C`MS`MSFT`JPM`ORCL`BIDU`SOHU
$ typestr syms;
STRING VECTOR
$ syms=syms$SYMBOL;
$ typestr syms;
FAST SYMBOL VECTOR

For more about the cast function please see cast.