SHAZAM Community
Ask Your Question
0

Round of decimal numbers

asked 2014-04-09 07:04:38 +0000

Pantera gravatar image

Hi all,

I have a variable y > 0 and some of the numbers are decimal numbers, for example 0.3, 1.6, 7.7 etc. I want to round of the numbers so they are hole numbers, i.e. 2.6 is round of to 3 and 1.2 is round of to 1 and 0.2 is round of to 0 etc. Any suggestion which GENR-commend could solve the problem?

Regards,

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-04-10 19:56:56 +0000

updated 2014-04-11 09:09:54 +0000

There is no direct rounding function however this can be calculated by use of the INT function. The INT function is used to extract the integer part of the decimal, but does not round e.g.

* Make a number
gen1 x = 1.956780

* Extract Integer part
gen1 y = INT(x)

Output:

 |_print x y
 X
    1.956780
 Y
    1.000000

To round to a specific number of decimal places do as follows:

* Make a number to round
gen1 x = 1.25678

* Define the number of dps as a rounding factor (3dps)
gen1 rnd = 0.001

* Round to the number of dps in the rounding factor
gen1 y = INT((x/rnd)+0.5) * rnd

Output:

 |_print x y
 X
    1.256780
 Y
    1.257000

To round to 0 dps the rounding factor is 1

gen1 x = 1.55648
gen1 rnd = 1
gen1 y = int((x*rnd)+0.5) / rnd
print y

Output:

 |_print y
 Y
    2.000000
edit flag offensive delete link more

Comments

Hi - thank for the suggestion. This solution function well if you want to round of a couple of numbers, but lets say you have a 1000 observations and want to round of all of them. Is it possible to round of a vector of decimal numbers?

Regards,

Pantera gravatar imagePantera ( 2014-04-11 03:54:53 +0000 )edit

Yes! You can change the GEN1 to GENR and it will work on the entire range.

GEN1 is the same as SAMPLE 1 1 and GENR.

Here is an example:

sample 1 5
read x 
1.556
3.1
4.9
8.23456
123.456
genr rnd = 1
genr y = int((x/rnd)+0.5) * rnd
print x y

Output:

 |_print x y
       X              Y
    1.556000       2.000000
    3.100000       3.000000
    4.900000       5.000000
    8.234560       8.000000
    123.4560       123.0000
SHAZAMHelp gravatar imageSHAZAMHelp ( 2014-04-11 04:25:23 +0000 )edit

Your suggestion function perfectly - thanks a lot!

Pantera gravatar imagePantera ( 2014-04-11 05:40:42 +0000 )edit

You also might consider handling something like this in Excel before reading the data file into Shazam. Excel has extensive data management capabilities.

krediker gravatar imagekrediker ( 2014-10-11 23:18:10 +0000 )edit

Thank Krediker, but do you know the Excel-code for round of? Anyway, Shazam is flexible and with help from the Shazam-team most of the problems can be solved.

Pantera gravatar imagePantera ( 2014-10-24 10:50:08 +0000 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2014-04-09 07:04:38 +0000

Seen: 237 times

Last updated: Apr 11 '14