syonip
syonip

Reputation: 2971

How to round the result of a division in intersystems cache?

What's the best way to round the result of a division in intersystems cache?

Thanks.

Upvotes: 0

Views: 1139

Answers (3)

Neo1989
Neo1989

Reputation: 21

To round down $SYSTEM.SQL.FLOOR(5/2) = 2

Upvotes: 0

David Whitten
David Whitten

Reputation: 484

In standard MUMPS (which Cache Object Script is backwards compatible with) there are three "division" related operators. The first is the single character "/" (i.e. forward slash). This is a real number divide. 5/2 is 2.5, 10.5/5 is 2.1, etc. This takes two numbers (each possibly including a decimal point and a fraction ) and returns a number possibly with a fraction. A useful thing to remember is that this numeric divide yields results that are as simple as they can be. If there are leading zeros in front of the decimal point like 0007 it will treat the number as 7. If there are trailing zeros after the decimal point, they will be trimmed as well. So 2.000 gets trimmed to 2 (notice no decimal point) and 00060.0100 would be trimmed to just 60.01

In the past, many implementors would guarantee that 3/3 would always be 1 (not .99999) and that math was done as exactly as could be done. This is not an emphasis now, but there used to be special libraries to handle Binary Coded Decimal, (BCD) to guarantee as close to possible that fractions of a penny were never generated.

The next division operator was the single character "\" (i.e. backward slash). this operator was called integer division or "div" by some folks. It would do the division and throw away any remainder. The interesting thing about this is that it would always result in an integer, but the inputs didn't have to be an integer. So 10\2 is 5, but 23\2.3 is 10 and so is 23.3\2.33 , If there would be a fraction left over, it is just dropped. So 23.3\2.3 is 10 as well. The full divide operator would give you many fractions. 23.3/2.3 is 10.130434 etc.

The final division operator is remainder (or "mod" or "modulo"), symbolized by the single character "#" (sometimes called hash, pound sign, or octothorpe). To get the answer for this one, the integer division "/" is calculated, and what ever is left over when an integer division is calculated will be the result. In our example of 23\2 the answer is 11 and the remaining value is 1, so 23#2 is 1 ad 23.3#2.3 is .3 You may notice that (number#divisor)+((number\divisior)*divisor) is always going to be your original number back.

Hope this helps you make this idea clear in your programming.

Upvotes: 1

DAiMor
DAiMor

Reputation: 3205

There are some functions, which used to format numbers, as well they would round it if necessary

$justify(expression,width[,decimal]) - Caché rounds or pads the number of fractional digits in expression to this value.

write $justify(5/3,0,3)
1.667

$fnumber(inumber,format,decimal)

write $fnumber(5/3,"",3)
1.667

$number(num,format,min,max)

write $number(5/3,3)
1.667

$normalize(num,scale)

w $normalize(5/3,3)
1.667

You just can choose which of them much more suitable for you. They doing different things, but result could be same.

Upvotes: 4

Related Questions