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.

3 comments:

  1. Thanks, just one question, I need to get the following:
    123.45 : using trunc = 123
    123.45 : using frac = 0.45
    I need the fractional part as integer:
    123.45 : using ??? = 45 not 0.45
    how to do that?

    ReplyDelete