Saturday, March 6, 2010

How to get the different parts of a real number

A real basically has 2 parts: the part before and the part behind the decimal separator. The integer part and the decimal part.
It's easy to get both parts separated, by using these functions: trunc and frac.



trunc

Truncates a real number by removing any decimal places.


Example


trunc (123.45) equals 123



frac

This function retrieves the decimal part of a real number.


Example


frac (123.45) equals 0.45


Note: The help section for function trunc says that 'numbers are always rounded down to a complete integer'.
Now if you've read my previous post about rounding in Ax, you may find this remark not to be completely true. Rounding to zero is closer to the truth.

Example

trunc ( -9.1 ) equals -9

while

rounddown (- 9.1 , 1) equals -10
roundzero ( -9.1 , 1) equals -9

On the other hand: rounddown ( -9.1 , -1) equals -9



Now I don't use these functions that often. But when I need them, I always have to go to the back of my head to remember their names.
So I thought I post them up here, as my online help.

No comments:

Post a Comment