cancel
Showing results for 
Search instead for 
Did you mean: 

round digits

Ninja_Li
New Contributor

Hi,

I would like to round the digits of a number column to a certain
precision, e.g. two decimal places (44.678 => 44.68). I tried to use
floor function but couldn't get it to work.

Please advise and thanks in advance.

Nick

7 REPLIES 7

Aaron_Davies
New Contributor
On Oct 1, 2009, at 10:31 PM, Ninja Li wrote:

> I would like to round the digits of a number column to a certain
> precision, e.g. two decimal places (44.678 => 44.68). I tried to use
> floor function but couldn't get it to work.

floor isn't round, it's floor--it truncates. depending on exactly what
you're doing, you may want to set the precision with P, or you may
want to use .Q.fmt:

q)P 4
q)44.678
44.68
q).Q.fmt[5;2;44.678]
"44.68"

Aaron, Thanks for your help. Could you please tell me where I can find anydocumentation on this function? I have googled but to no avail. NickOn Oct 1, 11:10�am, Aaron Davies wrote:> On Oct 1, 2009, at 10:31 PM, Ninja Li wrote:>> > � I would like to round the digits of a number column to a certain> > precision, e.g. two decimal places (44.678 => 44.68). I tried to use> > floor function but couldn't get it to work.>> floor isn't round, it's floor--it truncates. depending on exactly what �> you're doing, you may want to set the precision with \P, or you may �> want to use .Q.fmt:>> q)\P 4> q)44.678> 44.68> q).Q.fmt[5;2;44.678]> "44.68"


X-Mailer: Apple Mail (2.936)

https://code.kx.com/trac/wiki/DotQ/DotQDotfmt

login is anonymous/anonymous

Hi, if there a way to round it more than 2 digit? Say 4 digit / 6 digit? I've just check the document but doesn't aware it mentioned it

 

q)round:{[d;n]  ("j"$n*d) % d:xexp[10]d  }    

q)round[3]12.12345                           

12.123                                        

q)round[3]12.12355                            

12.124                                        

 

HTH,

 

Kim

 

Von: personal-kdbplus@googlegroups.com [mailto:personal-kdbplus@googlegroups.com] Im Auftrag von Carfield Yim
Gesendet: Dienstag, 5. Januar 2016 10:20
An: Kdb+ Personal Developers
Betreff: Re: [personal kdb+] Re: round digits

 

Hi, if there a way to round it more than 2 digit? Say 4 digit / 6 digit? I've just check the document but doesn't aware it mentioned it

Nick
New Contributor
it is often useful to round to values other than powers of 10:

rnd:{x*"j"$y%x}

q).01 .02 .05 rnd\: 10?1f
0.02 0.36 0.03 0.69 0.64 0.04 0.9 0.79 0.2 0.78
0.02 0.36 0.02 0.68 0.64 0.04 0.9 0.8  0.2 0.78
0    0.35 0.05 0.7  0.65 0.05 0.9 0.8  0.2 0.8

q)00:01 00:05 00:10 rnd\: 10?1u
00:18 00:24 00:51 00:58 00:52 00:17 00:54 00:27 00:44 00:15
00:20 00:25 00:50 01:00 00:50 00:15 00:55 00:25 00:45 00:15
00:20 00:20 00:50 01:00 00:50 00:20 00:50 00:30 00:40 00:20

scientific notation can be used to retain the ability to compactly specify the number of decimals:

q)rnd[1e-5] 5?1f
0.49478 0.86656 0.6415 0.90827 0.97961







there's a builtin string rounder that's used by .Q.fmt

q)3 .Q.f'12.12345 12.12355
"12.123"
"12.124"
q)

you can use "get" (or "F"$, etc.) on the result if you want it back as
a number, not a string

there's also xbar, but that effectively uses floor, not rounding, and
it has some type issues