Question Paul Riker · Nov 4, 2016 Round to next whole number #ObjectScript #Ensemble How can I round to the next whole number? .1 = 11.2 = 21.7 = 2
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).
Michel Bruyère Aug 9, 2019 to Timothy Leavitt Don't rely too much on this !w $System.SQL.CEILING(1.000000000000000001)2w $System.SQL.CEILING(1.0000000000000000001)1
Adam Skurek Aug 12, 2019 to Michel Bruyère Not a problem with the function itself.w 1.00000000000000000011
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
Paul Gausden · Nov 8, 2016 If you have limited decimal places, there's always the old MUMPS way F I=0.1:0.1:10 W !,I,?5,$J(I+.49999999999,0,0)
John Matson · Nov 8, 2016 You could also use plain ol' integer division plus 1.USER>w .1\1+11USER>W 1.2\1+12USER>W 1.7\1+12USER>[EDIT] As pointed out by Carlos Lopes, this method fails for integer input:USER>W 1\1+12
John Matson Nov 9, 2016 to Carlos Lopes It sure would. I hadn't considered integer input. Thanks for pointing that out!
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
Julius Kavay · Aug 10, 2019 set a=1.2 // your numberwrite a\1+''(a#1) // this is a NOT NOT and not a quote!
Here's one lazy option:
If you look at the implementation of that method in %SYSTEM.SQL, you'll see:
So that's another option (although messier).
Perfect, Thanks!
Don't rely too much on this !
w $System.SQL.CEILING(1.000000000000000001)
2
w $System.SQL.CEILING(1.0000000000000000001)
1
Not a problem with the function itself.
w 1.00000000000000000011
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
If you have limited decimal places, there's always the old MUMPS way
F I=0.1:0.1:10 W !,I,?5,$J(I+.49999999999,0,0)
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
This approach would fail for integers.
USER>Write 1\1+1
2
It sure would. I hadn't considered integer input. Thanks for pointing that out!
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
set a=1.2 // your number
write a\1+''(a#1) // this is a NOT NOT and not a quote!