Skip to main content Link Search Menu Expand Document (external link)

truncate() : Remove Decimal Portion


On this page

Summary

Remove the decimal portion of a number without rounding.

Signature

truncate(value)

Parameters

Parameter Type Required Description
value Number Yes The number to truncate

Returns

Type: Integer

The integer portion of the number.


Examples

Remove Decimals

<p>Whole number: </p>

Data:

doc.Params["model"] = new {
    value = 19.99m
};

Output:

<p>Whole number: 19</p>

Extract Integer Part

<p>Dollars: $</p>
<p>Cents: ¢</p>

Data:

doc.Params["model"] = new {
    price = 19.75m
};

Output:

<p>Dollars: $19</p>
<p>Cents: 75¢</p>

No Rounding Behavior


  <li></li>

Data:

doc.Params["model"] = new {
    values = new[] { 1.1, 2.5, 3.9, 4.0, -2.8 }
};

Output:

<li>1.1 → 1</li>
<li>2.5 → 2</li>
<li>3.9 → 3</li>
<li>4.0 → 4</li>
<li>-2.8 → -2</li>

Notes

  • Simply removes decimal portion (no rounding)
  • Returns integer type
  • Different from floor() for negative numbers
  • truncate(-2.8) = -2, but floor(-2.8) = -3
  • For rounding, use round(), ceiling(), or floor()
  • Same as int() function behavior

See Also