Skip to main content Link Search Menu Expand Document Toggle dark mode Copy Code (external link)

truncate() : Remove Decimal Portion

Remove the decimal portion of a number without rounding.


On this page

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: {{truncate(model.value)}}</p>

Data:

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

Output:

<p>Whole number: 19</p>

Extract Integer Part

<p>Dollars: ${{truncate(model.price)}}</p>
<p>Cents: {{round((model.price - truncate(model.price)) * 100)}}ยข</p>

Data:

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

Output:

<p>Dollars: $19</p>
<p>Cents: 75ยข</p>

No Rounding Behavior

{{#each model.values}}
  <li>{{this}} โ†’ {{truncate(this)}}</li>
{{/each}}

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