Round to next whole number

How can I round to the next whole number?

 

.1 = 1

1.2 = 2

1.7 = 2

  • 0
  • 0
  • 538
  • 6
  • 5

Answers

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).

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.0000000000000000001

1

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 smiley

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

wink

set a=1.2 // your number

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