How to round to an Integer

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to round to an Integer

John Harney
Recently figured this out.  Seems to work fine

trim(trim(round(1.111,0),'0'),'.')   = 1





________________________________
Aviso de Privacidad y Confidencialidad // Privacy and Confidentiality Notice // Avis de confidentialit?: https://privacy.grupobimbo.com/
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: How to round to an Integer

Richard Hipp-3
On 10/18/18, John Harney <[hidden email]> wrote:
> Recently figured this out.  Seems to work fine
>
> trim(trim(round(1.111,0),'0'),'.')   = 1
>

CAST(1.111 AS integer)

--
D. Richard Hipp
[hidden email]
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: How to round to an Integer

CedricCicada
I don't think the cast rounds, though.  It just truncates.  Am I wrong?

RobR

On Thu, Oct 18, 2018 at 4:13 PM Richard Hipp <[hidden email]> wrote:

> On 10/18/18, John Harney <[hidden email]> wrote:
> > Recently figured this out.  Seems to work fine
> >
> > trim(trim(round(1.111,0),'0'),'.')   = 1
> >
>
> CAST(1.111 AS integer)
>
> --
> D. Richard Hipp
> [hidden email]
> _______________________________________________
> sqlite-users mailing list
> [hidden email]
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
>
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: How to round to an Integer

Keith Medcalf
In reply to this post by Richard Hipp-3

On Thursday, 18 October, 2018 14:13, Richard Hipp <[hidden email]> wrote:

>On 10/18/18, John Harney <[hidden email]> wrote:

>> Recently figured this out.  Seems to work fine

>> trim(trim(round(1.111,0),'0'),'.')   = 1

>CAST(1.111 AS integer)

That should be CAST(round(x,0) as integer) if you want the rounded result as an actual integer (the bare CAST truncates).  Note however that the default builtin round() function does "round half away from 0" and not "round half to even" rounding, so beware of using the result in further calculations ...

sqlite> select cast(1.111 as integer);
1
sqlite> select cast(1.5 as integer);
1
sqlite> select cast(1.6 as integer);
1
sqlite> select cast(round(1.6,0) as integer);
2
sqlite> select cast(round(1.5,0) as integer);
2
sqlite> select cast(round(2.5,0) as integer);
3
sqlite> select cast(roundhe(2.5,0) as integer);
2

---
The fact that there's a Highway to Hell but only a Stairway to Heaven says a lot about anticipated traffic volume.





_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users