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, decimal32, decimal64, decimal128.

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()=="";
// output
1

string(10);
// output
10

typestr string(108.5);
// output
STRING

string(now());
// output
2016.03.02T20:55:31.287

bool

x=bool();
x;
// output
00b
typestr x;
// output
BOOL

bool(`true);
// output
1
bool(`false);
// output
0

bool(100.2);
// output
1

bool(0);
// output
0

int

x=int();
x;
// output
00i
typestr x;
// output
INT

int(`10.9);
// output
10

int(2147483647);
// output
2147483647

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

short

x=short();
x;
// output
00h
typestr x;
// output
SHORT

short(`12.3);
// output
12

short(`120.9c);
// output
120

short(32767);
// output
32767


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

long

x=long();
x;
// output
00l
typestr x;
// output
LONG

long(`10.9);
// output
10

long(9223372036854775807l);
// output
9223372036854775807

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

char

x=char();
x;
// output
00c
typestr x;
// output
CHAR

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

double

x=double();
x;
// output
00F
typestr x;
// output
DOUBLE

typestr double(`10);
// output
DOUBLE

double(`10.9);
// output
10.9

double(now());
// output
5.297834e+011

date

date();
// output
00d

date(`2011.10.12);
// output
2011.10.12

date(now());
// output
2016.03.02

datetime

datetime(2009.11.10);
// output
2009.11.10T00:00:00
typestr datetime(2009.11.10);
// output
DATETIME

datetime(now());
// output
2016.03.02T20:51:10

timestamp

timestamp(2016.10.12);
// output
2016.10.12T00:00:00.000

timestamp(2016.10.12)+1;
// output
2016.10.12T00:00:00.001

timestamp(now());
// output
2016.10.13T20:28:45.104

month


 month();

0M


 month(`2012.12m);

012.12M


/make a month variable from date

 month(2012.12.23);

012.12M

//make a month variable from timestamp

 month(now());

016.03M

second

second();
// output
00s

second("19:36:12");
// output
19:36:12

second(now());
// output
20:50:31

minute

minute();
// output
00m

minute(now());
// output
20:49m

time

time();
// output
00t

time("12:32:56.356");
// output
12:32:56.356

time(now());
// output
20:49:12.564

symbol

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

x;
// output
["AMZN","AAPL","GOOG","FB","SNAP"]

typestr x;
// output
STRING VECTOR

y=symbol(x);

y;
// output
["AMZN","AAPL","GOOG","FB","SNAP"]

typestr y;
// output
FAST SYMBOL VECTOR

cast(X, dataTypeName) / $

x=3.1;
typestr x;
// output
DOUBLE

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

x;
// output
3
typestr x;
// output
INT

19.99$INT;
// output
20

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

For more about the cast function please see cast.