# C++ convert large decimal to KDB

**URL:** <https://forum.kx.com/t/c-convert-large-decimal-to-kdb/13420>\
**Category:** Community Support\
**Tags:** imported, kdb-and-q\
**Created:** [October 4, 2021, 12:00am UTC](https://forum.kx.com/t/c-convert-large-decimal-to-kdb/13420 "2021-10-04T00:00:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ekallivrousis](https://avatars.discourse-cdn.com/v4/letter/e/ecd19e/32.png) [@ekallivrousis](https://forum.kx.com/u/ekallivrousis)\
**Post date:** [October 4, 2021, 12:00am UTC](https://forum.kx.com/t/c-convert-large-decimal-to-kdb/13420/1 "2021-10-04T00:00:00Z")

</div>

[https://learninghub.kx.com/forums/topic/c-convert-large-decimal-to-kdb](https://learninghub.kx.com/forums/topic/c-convert-large-decimal-to-kdb)

I need help to find out if there is compatible function in c++ class used to connect to db that would convert the large(24) decimal place to datatype compatible to kdb

---

<div class="post-metadata">

**Author:** ![rocuinneagain](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/rocuinneagain/32/287_2.png) [@rocuinneagain](https://forum.kx.com/u/rocuinneagain)\
**Post date:** [October 26, 2021, 12:00am UTC](https://forum.kx.com/t/c-convert-large-decimal-to-kdb/13420/2 "2021-10-26T00:00:00Z")

</div>

These are examples of using the C interface to extend the Kdb+ database.

&nbsp;

Documentation is here:

[https://code.kx.com/q/interfaces/using-c-functions/](https://code.kx.com/q/interfaces/using-c-functions/")

It shows an example of writing a new ‘add’ function in C:

```
#include"k.h" 
#ifdef __cplusplus extern "C"{ #endif K add(K x,K y) { if(x->t!=-KJ||y->t!=-KJ) 
     return krr("type"); 
     return kj(x->j+y->j); } 
#ifdef __cplusplus } #endif
```

Then this can be used from inside a Kdb+ process:

```
q)add:(`add 2:(`add;2)) 
q)add[3;4] 
7
```

This is a possible method to add math functions for 128-bit floating point numbers inside Kdb+.

---

<div class="post-metadata">

**Author:** ![dcrossey2](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@dcrossey2](https://forum.kx.com/u/dcrossey2)\
**Post date:** [October 5, 2021, 12:00am UTC](https://forum.kx.com/t/c-convert-large-decimal-to-kdb/13420/3 "2021-10-05T00:00:00Z")

</div>

Hi @ekallivrousis,

You could use the Double / Float type as per the [C API Reference](https://code.kx.com/q/interfaces/capiref/")

Thanks for posting your query on the forums!

Kind regards,

David

---

<div class="post-metadata">

**Author:** ![ekallivrousis](https://avatars.discourse-cdn.com/v4/letter/e/ecd19e/32.png) [@ekallivrousis](https://forum.kx.com/u/ekallivrousis)\
**Post date:** [October 21, 2021, 12:00am UTC](https://forum.kx.com/t/c-convert-large-decimal-to-kdb/13420/4 "2021-10-21T00:00:00Z")

</div>

Hey rocuinneagain,

Correct me if im wrong but this is to do math in C correct? I want to do math operations in the KDB database. The c program will be just putting the data into DB

---

<div class="post-metadata">

**Author:** ![ekallivrousis](https://avatars.discourse-cdn.com/v4/letter/e/ecd19e/32.png) [@ekallivrousis](https://forum.kx.com/u/ekallivrousis)\
**Post date:** [November 2, 2021, 12:00am UTC](https://forum.kx.com/t/c-convert-large-decimal-to-kdb/13420/5 "2021-11-02T00:00:00Z")

</div>

Thank you for the above this is a big step in right direction for us.

if we store large decimal will precision be lost when storing in kdb and querying to display on ui via java

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![lkerr2](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/lkerr2/32/57_2.png) [@lkerr2](https://forum.kx.com/u/lkerr2)\
**Post date:** [October 5, 2021, 12:00am UTC](https://forum.kx.com/t/c-convert-large-decimal-to-kdb/13420/6 "2021-10-05T00:00:00Z")

</div>

Hi David,  
To elaborate on @ekallivrousis comment, we are trying to use 128-bit floating point data type (instead of 64-bit(8 byte double) ) to support 24 decimal places instead of 16 decimals supported by double. Currently, as per [C API Reference](https://code.kx.com/q/interfaces/capiref/") KDB+ supports 4byte(float) and 8byte(double). Is there were way to store 128-bit floating point values ?

---

<div class="post-metadata">

**Author:** ![rocuinneagain](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/rocuinneagain/32/287_2.png) [@rocuinneagain](https://forum.kx.com/u/rocuinneagain)\
**Post date:** [October 5, 2021, 12:00am UTC](https://forum.kx.com/t/c-convert-large-decimal-to-kdb/13420/7 "2021-10-05T00:00:00Z")

</div>

You can see an example of mapping a decimal to either a list of bytes or scaling to a double in this [interface](https://github.com/KxSystems/arrowkdb/blob/751d6e6cfc3cbfab76628eb4a8368f3493ad3cbc/src/ArrayReader.cpp#L297 ").
