## Recommended Posts

Is there a function to round UP a decimal to the nearest integer?

I am in need of rounding numbers like 1.415 to 2, 3.46 to 4, etc.

Any help with getting my G-Code more accurate would be greatly appreciated. Thanks.

##### Share on other sites

MP has a round( ) function, but I don't see any way to ensure you're always rounding up.   There is also the rounding method in the format statement, but based on the definition below I don't think it will help you

Rounding method. Use the following values:

• (omitted) - Normal rounding.
• 2 - Round final decimal to nearest even number.
• 5 - Round final decimal to nearest multiple of 5.
• 9 - Truncate the decimal portion of the value at the specified number of digits.

Below is an example that should round the passed in value up (or down), but I can't test it.  Maybe it will help you.

```[email protected]    : 1
[email protected]  : 2

[email protected] : -99999

value      : 0
type       : [email protected]

int_value  : 0
frac_value : 0

#//The first argument is being passed by reference '!'

#//'type' is initialized to [email protected](1) and set to [email protected](1) at the end of 'pround'
#//Because of this, we can consider 'type' optional
pround(!value, type)
#//Get the integer portion of the passed in value
int_value = int(value)

#//Get the fractional portion of the passed in value
#//The fractional portion is not used in this example
frac_value = frac(value)

if type = [email protected],
[
value = int_value + 1
]
if type = [email protected],
[
value = abs_value
]
else,
[
value = [email protected]
]

type = [email protected]

#//Calling example
#//Output (depending on format) ->
#//        x_abs is             : 4.75342
#//        x_abs rounded up is  : 5
#//        x_abs rounded down is: 4
#//
#//        x_abs rounded up is  : 5

x_abs : 0
y_abs : 0
z_abs : 0

pany_postblock
x\$ = 4.75342
x_abs = vequ(x\$)

"x_abs is             :", *x_abs, e\$
pround(!x_abs, [email protected])
"x_abs rounded up is  :", *x_abs, e\$

x_abs = vequ(x\$)
pround(!x_abs, [email protected])
"x_abs rounded down is:", *x_abs, e\$
*e\$

#//The 'type' argument is optional
x_abs = vequ(x\$)
pround(!x_abs)
"x_abs rounded up is  :", *x_abs, e\$```

• 1

##### Share on other sites
19 hours ago, Henk said:

First add 0.50, and then do the normal round.

I was close, but not correct:    2.0 + 0.5 = 2.5, will be round to 3.0

So first check for already being an integer.

##### Share on other sites

Thanks for the replies. The problem is not knowing which way to round.

I appreciate the example on how to use a function Jeff.

I will do something similar with a bunch of if statements to get the output I need.

##### Share on other sites
10 hours ago, cncah said:

Thanks for the replies. The problem is not knowing which way to round.

I appreciate the example on how to use a function Jeff.

I will do something similar with a bunch of if statements to get the output I need.

I found an error in the code snippet I posted previously, a corrected version is below.

```[email protected]    : 1
[email protected]  : 2

[email protected] : -99999

value      : 0
type       : [email protected]

int_value  : 0
frac_value : 0

#//The first argument is being passed by reference '!'

#//'type' is initialized to [email protected](1) and set to [email protected](1) at the end of 'pround'
#//Because of this, we can consider 'type' optional
pround(!value, type)
#//Get the integer portion of the passed in value
int_value = int(value)

#//Get the fractional portion of the passed in value
#//The fractional portion is not used in this example
frac_value = frac(value)

if type = [email protected],
[
value = int_value + 1
]
else, if type = [email protected],
[
value = abs_value
]
else,
[
value = [email protected]
]

type = [email protected]

#//Calling example
#//Output (depending on format) ->
#//        x_abs is             : 4.75342
#//        x_abs rounded up is  : 5
#//        x_abs rounded down is: 4
#//
#//        x_abs rounded up is  : 5

x_abs : 0
y_abs : 0
z_abs : 0

pany_postblock
x\$ = 4.75342
x_abs = vequ(x\$)

"x_abs is             :", *x_abs, e\$
pround(!x_abs, [email protected])
"x_abs rounded up is  :", *x_abs, e\$

x_abs = vequ(x\$)
pround(!x_abs, [email protected])
"x_abs rounded down is:", *x_abs, e\$
*e\$

#//The 'type' argument is optional
x_abs = vequ(x\$)
pround(!x_abs)
"x_abs rounded up is  :", *x_abs, e\$```

It's also worth noting that this example will only work for positive numbers.  It would need to be modified to round positive and negative numbers correctly.

## Create an account

Register a new account