Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

Why does converting a decimal number to an integer drop the fractional part?

Quick answer

A decimal’s fractional part disappears when the conversion operation discards it, but “convert to integer” does not specify one universal rule. In JavaScript, Math.trunc() removes fractional digits toward zero, while Math.round() selects the nearest integer, with exact halves going toward positive infinity. Other operations or languages may behave differently or reject the input. Identify the exact operation and test positive, negative, and halfway values.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

Why does converting a decimal number to an integer drop the fractional part? I expected 13.8 to become 14.

Geez

Because some integer conversions truncate rather than round. Truncation removes the digits after the decimal point and moves toward zero: in JavaScript, Math.trunc(13.8) gives 13, not 14. Rounding is a separate rule: JavaScript Math.round(13.8) gives 14. [S1] [S2]

The word “convert” alone isn’t enough to identify the behavior. Check the specific function, operator, library, or input-reading method your code uses; different operations can have different rules. If you share the language and the exact expression, the behavior can be narrowed down.

Follow-up question

How can I tell whether my result is truncation or rounding?

Geez

Try a small diagnostic set and compare the outputs with the rules you intended. For example, with JavaScript’s Math.trunc(), 13.8 becomes 13 and −13.8 becomes −13: both move toward zero. With Math.round(), 13.8 becomes 14, while −13.8 becomes −14. The halfway cases also help identify the exact rule: JavaScript Math.round() sends 5.5 to 6 and −5.5 to −5, because exact halves go toward positive infinity. [S1] [S2]

Write down the actual operation alongside each result. A useful comparison is:

  • Truncation: 13.8 → 13; −13.8 → −13.
  • JavaScript Math.round(): 13.8 → 14; −13.8 → −14; 5.5 → 6; −5.5 → −5.

These examples describe those specific JavaScript methods, not every language’s conversion behavior. A different operation could use another rule.

Follow-up question

What if the conversion throws an error instead of returning an integer?

Geez

That can mean the operation did not accept the value or input as supplied, rather than silently discarding its fractional part. The JavaScript references here explain Math.trunc() and Math.round(); they do not establish what every parser, type conversion, or language does with invalid or nonnumeric input. Don’t infer a general rejection rule from these examples.

To investigate, record the exact expression, the input’s type and displayed value, the output or full error message, and the language or runtime. Check the documentation for that precise operation, especially how it handles decimal values and invalid input. If you change the conversion method, test the same inputs again so you can see whether the new behavior matches your requirement.

Follow-up question

Can you show a short example of choosing deliberately between truncating and rounding?

Geez

Assuming your code is JavaScript and you want to make the choice explicit, call the method that matches your intended rule:

const value = 13.8;

const truncated = Math.trunc(value); // 13: removes the fractional part
const rounded = Math.round(value);   // 14: rounds to the nearest integer

Before relying on this in a larger calculation, check the rule for negative and halfway inputs too. For JavaScript, Math.trunc() moves toward zero, and Math.round() sends exact halves toward positive infinity. [S1] [S2] MDN also cautions that bitwise shortcuts are not interchangeable with Math.trunc() in all cases because they convert to 32-bit integers and can overflow. [S1]

Use the following checklist: name the operation; try positive and negative decimals; include a halfway value if rounding is intended; compare the result with the documented rule; and confirm how invalid input is handled. The cited material does not establish behavior for every language, conversion method, or input type, so check those separately.

A practical checklist

  • Record the exact conversion function, operator, or input-reading method.

  • Test a positive decimal and a negative decimal, such as 13.8 and −13.8.

  • If you expect rounding, test an exact halfway value such as 5.5 and check the documented tie rule.

  • Compare each result with the operation’s documentation rather than assuming all integer conversions behave alike.

  • If the operation errors, note the input, its type, and the complete error message, then check that operation’s invalid-input behavior.

A few more useful answers

Does truncation always mean rounding down?

No. In JavaScript, Math.trunc() moves toward zero, so −13.8 becomes −13. That is different from always choosing the lower integer. [S1]

Does every language round halfway values the same way?

No. JavaScript Math.round() sends exact halves toward positive infinity; do not assume another language or operation uses that rule. Check its documentation. [S2]

Are bitwise shortcuts a safe substitute for Math.trunc() in JavaScript?

Not in all cases. MDN notes that bitwise shortcuts convert to 32-bit integers and can overflow, so use the documented operation that matches your intent. [S1]

Sources

References used for this guide.

  1. [S1] Math.trunc() — JavaScript | MDN
    developer.mozilla.org · Checked
  2. [S2] Math.round() — JavaScript | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work