How do i convert a DECIMAL to a DOUBLE

97 views
Skip to first unread message

Adil Baig

unread,
Jan 6, 2017, 1:08:44 PM1/6/17
to DataStax C++ Driver for Apache Cassandra User Mailing List
I have a schema like this

CREATE TABLE price.price (
ric text,
date date,
close decimal,
created_at timestamp,
high decimal,
low decimal,
open decimal,
security_id int,
volume decimal,
PRIMARY KEY (ric, date)
)

I would like to fetch the `close` column and display it in a C++ application.

Here's the relevant code:

const cass_byte_t* close;
int scale = 0;
cass_value_get_decimal(cass_row_get_column_by_name(row, "close"), &close, &size, &scale);
cout << atof((char*)close) << endl;
printf("CLOSE: '%.*s'\n", (int)size, close);

The cout line prints 0, the printf prints garbage. How do i fetch the value of a decimal field?

Michael Penick

unread,
Jan 12, 2017, 3:07:10 PM1/12/17
to cpp-dri...@lists.datastax.com
The cpp-driver doesn't currently handle this, but it can be done in a fairly straightforward way using a third party multiple precision library like gmplib.

Here's an example using gmplib from the php-driver:

First your application will need to take the "close" byte buffer and convert that  to a "mpz_t" value: https://github.com/datastax/php-driver/blob/master/ext/util/math.c#L555-L572

Then you can scale that bigint by using using "scale" and a "mpf_t":  https://github.com/datastax/php-driver/blob/master/ext/src/Cassandra/Decimal.c#L28-L47

You can then call "mpf_get_d()" to get a double from a "mpf_t".

Mike


--
You received this message because you are subscribed to the Google Groups "DataStax C++ Driver for Apache Cassandra User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cpp-driver-user+unsubscribe@lists.datastax.com.

Reply all
Reply to author
Forward
0 new messages