Written by

Question Paul Riker · Nov 4, 2016

Round to next whole number

How can I round to the next whole number?

.1 = 1

1.2 = 2

1.7 = 2

Comments

Timothy Leavitt · Nov 4, 2016

Here's one lazy option:

USER>w $System.SQL.CEILING(.1)
1
USER>w $System.SQL.CEILING(1.2)
2
USER>w $System.SQL.CEILING(1.7)
2

If you look at the implementation of that method in %SYSTEM.SQL, you'll see:

$s('$isvalidnum(val):"",+val[".":+$p(val,".")+(val>0),1:+val)

So that's another option (although messier).

0
Paul Riker  Nov 4, 2016 to Timothy Leavitt

Perfect, Thanks!

0
Michel Bruyère  Aug 9, 2019 to Timothy Leavitt

Don't rely too much on this !

w $System.SQL.CEILING(1.000000000000000001)

2

w $System.SQL.CEILING(1.0000000000000000001)

1

0
Adam Skurek  Aug 12, 2019 to Michel Bruyère

Not a problem with the function itself.

w 1.0000000000000000001

1

0
Eduard Lebedyuk · Nov 4, 2016

Ceiling, floor, $NORMALIZE. Examples:

>Write $SYSTEM.SQL.CEILING(.1)
1
>Write $SYSTEM.SQL.CEILING(1.2)
2
>Write $SYSTEM.SQL.CEILING(1.7)
2
>Write $SYSTEM.SQL.FLOOR(.1)
0
>Write $SYSTEM.SQL.FLOOR(1.2)
1
>Write $SYSTEM.SQL.FLOOR(1.7)
1
>Write $NORMALIZE(.1, 0)
0
>Write $NORMALIZE(1.2, 0)
1
>Write $NORMALIZE(1.7, 0)
2
0
Paul Gausden · Nov 8, 2016

If you have limited decimal places, there's always the old MUMPS way smiley

F I=0.1:0.1:10  W !,I,?5,$J(I+.49999999999,0,0)

0
John Matson · Nov 8, 2016

You could also use plain ol' integer division plus 1.

USER>w .1\1+1
1
USER>W 1.2\1+1
2
USER>W 1.7\1+1
2
USER>

[EDIT]  As pointed out by Carlos Lopes, this method fails for integer input:

USER>W 1\1+1
2

0
Carlos Lopes  Nov 9, 2016 to John Matson

This approach would fail for integers.

USER>Write 1\1+1

2

0
John Matson  Nov 9, 2016 to Carlos Lopes

It sure would.  I hadn't considered integer input.  Thanks for pointing that out!

0
Robert Cemper  Aug 9, 2019 to Carlos Lopes

but that way

USER>f a=0.8:.1:2.2  w a,?7,a\1+(a#1>0*1),?10,!
.8     1
.9     1
1      1
1.1    2
1.2    2
1.3    2
1.4    2
1.5    2
1.6    2
1.7    2
1.8    2
1.9    2
2      2
2.1    3
2.2    3

wink

0
Julius Kavay · Aug 10, 2019

set a=1.2 // your number

write a\1+''(a#1)   // this is a NOT NOT and not a quote!

0