# Function in JavaScript equivalent to Frac do Delphi

• I need a JavaScript function that returns the fractional part of a floating point number.

Example:

``````if Frac( num1 / num2 ) > 0 then begin
``````

• Searching for documentation Delphi found this function http://docwiki.embarcadero.com/Libraries/Sydney/en/System.Frac .
The following section of the documentation:

# Description

Returns the fractional part of a real number.

In the Delphi code, the function `Frac` returns the fractional part of argument `X`.

`X` It's a real expression. The result is the fractional part of `X`;
that is, `Frac(X) = X - Int (X)`.

Then three things should be observed:

• In javascript the number guy https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Number is the closest to the Delphi type http://docwiki.embarcadero.com/Libraries/Sydney/en/System.Extended .
• Function documentation shows its operation `Frac(X) = X - Int (X)`.
• The Delphi function http://docwiki.embarcadero.com/Libraries/Sydney/en/System.Int returns the entire part of a real number that is equivalent to javascript https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc .

Then a possible alternative in emulating in javascript the function `Frac(X)` of Delphi would be:

``````//Frac(X) = X - Int (X)
const num1 = 13;
const num2 = 3;
function frac(x) {
return x - Math.trunc(x);
}
Note:

The function was not used https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/parseInt#description for
function converts your first argument to a string before returning the whole.

• I have fixed this two methods on my repository backup, please consider conver them to function's.

``````                trunc(x) {
return x > 0 ? Math.floor(x) : Math.ceil(x);
}

fracc(x) {
return x > 0 ? x - Math.floor(x) : x - Math.ceil(x);
}``````

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2