KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ Rounding in select statement Re: Rounding in select statement

  • mauricelim

    Member
    November 26, 2021 at 12:00 am

    You could do something like this:

    q)fix:{.Q.fmt'[x+1+count each string floor y;x;y]} 
    q)select time,sym,fix[1]price from trade 
    time sym price 
    ------------------------ 
    09:30:00.000 a "10.8" 
    09:31:00.000 a "11.8" 
    09:32:00.000 a "13.2" 
    09:30:00.000 b "100.8" 
    09:31:00.000 b "107.0" 
    09:32:00.000 b "124.0" 
    
    q)select time,sym,"F"$fix[1]price from trade 
    time       sym price 
    ---------------------- 
    09:30:00.000 a 10.8 
    09:31:00.000 a 11.8 
    09:32:00.000 a 13.2 
    09:30:00.000 b 100.8 
    09:31:00.000 b 107 
    09:32:00.000 b 124

    Reference: https://code.kx.com/q/ref/dotq/#qfmt-format

    Note that if you do cast it back to a float, the decimal does not show for .0