Safe and correct way to convert REAL to Int

mktdata
mktdata Contributor
edited May 20 in TREP APIs

Hello, We use RFA 8.1 C++ api. We have a use case where we want to convert REAL type to int using DataBuffer. We accept the fact that doing this will result in data truncation. I can do it in two ways but what is the most accurate way to do it?

Assuming DataBuffer contains REAL type -

A. dataBuffer.getInt()

B. static_cast<int>(dataBuffer.getDouble());

Which one of the above is more accurate? Is there any other way to do this?

Answers

  • mktdata
    mktdata Contributor

    I just tried something and realized that following does not work correctly

    dataBuffer.getInt()

  • Jirapongse
    Jirapongse ✭✭✭✭✭

    @mktdata

    Thank you for reaching out to us.

    It could look like this:

    			write("\n%s\n", dataBuffer.getAsString().c_str());						
    			write("%lld\n", dataBuffer.getInt());			
    			write("%d\n", (int)round(dataBuffer.getDouble()));
    

    If you would like to get a round number, you can use the round method in <cmath>.

  • mktdata
    mktdata Contributor
    edited May 20

    HI @Jirapongse When value is "45.110" then dataBuffer.getInt() returns 45110 which is not expected in our case I guess.

    Also REAL64 can have exponent part, so how would it work in that case?

  • Jirapongse
    Jirapongse ✭✭✭✭✭

    @mktdata

    I used this code.

    		case DataBuffer::RealEnum:
    			printf("\n%s\n", dataBuffer.getAsString().c_str());						
    			printf("%lld\n", dataBuffer.getInt());
    			printf("%d\n", (int)round(dataBuffer.getDouble()));
    			break;
    

    It returned the correct values.

    2025 May 20 17:26:32.441 ST GMT+07:00 998C 8F08   594 TRACE <- Received MMT_MARKET_PRICE Update JPY=.
    FieldList: standard data count = 2
    FieldEntry [ 22] BID
    45.110
    45
    45

    I am using rfa8.2.5.L3.