How do I convert an integer to decimal in JavaScript?
Categories:
Converting Integers to Decimals in JavaScript

Learn various methods to convert integer values into decimal representations in JavaScript, covering common use cases and best practices.
In JavaScript, integers are technically a subset of floating-point numbers (doubles). However, when you need to explicitly represent an integer with a decimal component, such as displaying currency or fixed-precision values, you often need to format it. This article explores several common techniques to achieve this, from simple division to more robust formatting methods.
Basic Division for Decimal Conversion
The most straightforward way to convert an integer to a decimal, especially when you know the desired scale (e.g., two decimal places for currency), is through simple division. If your integer represents cents and you want dollars, you divide by 100. This method is effective for fixed-scale conversions.
const cents = 250;
const dollars = cents / 100; // Result: 2.5
const largeInteger = 12345;
const decimalValue = largeInteger / 1000; // Result: 12.345
Using division to convert an integer to a decimal.
decimal.js
or big.js
.Using toFixed()
for Fixed-Point Notation
The toFixed()
method is a powerful tool for formatting numbers to a specific number of decimal places. It returns a string representation of the number, not a number type. This is particularly useful for display purposes, such as currency or percentages.
const integerValue = 123;
const fixedTwoDecimals = (integerValue / 100).toFixed(2); // Result: "1.23"
const anotherInteger = 5000;
const fixedFourDecimals = (anotherInteger / 1000).toFixed(4); // Result: "5.0000"
const singleDigit = 7;
const fixedOneDecimal = (singleDigit / 10).toFixed(1); // Result: "0.7"
Applying toFixed()
after division for precise decimal representation.
toFixed()
method returns a string. If you need to perform further mathematical operations, you'll need to convert it back to a number using parseFloat()
or the unary plus operator +
.Leveraging Intl.NumberFormat
for Localization
For more advanced and localized decimal formatting, especially when dealing with different currency symbols, decimal separators, or thousands separators, Intl.NumberFormat
is the recommended approach. It provides robust internationalization capabilities.
const amountInCents = 12345;
const amountInDollars = amountInCents / 100;
// Format as USD currency
const formatterUSD = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});
console.log(formatterUSD.format(amountInDollars)); // Example: "$123.45"
// Format as EUR currency
const formatterEUR = new Intl.NumberFormat('de-DE', {
style: 'currency',
currency: 'EUR',
});
console.log(formatterEUR.format(amountInDollars)); // Example: "123,45 €"
// Format as a simple decimal with specific precision
const formatterDecimal = new Intl.NumberFormat('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
console.log(formatterDecimal.format(amountInDollars)); // Example: "123.45"
Using Intl.NumberFormat
for localized decimal formatting.
flowchart TD A[Integer Input] --> B{Determine Desired Decimal Places} B --> C{Divide by Power of 10} C --> D{Apply `toFixed()` (for display)} D --> E[String Output] C --> F{Apply `Intl.NumberFormat` (for localization)} F --> G[Localized String Output] A --> H{Direct Conversion (e.g., `parseFloat()`)} H --> I[Number Output (may have floating point issues)]
Flowchart illustrating different paths for integer to decimal conversion.
Converting String Decimals Back to Numbers
After using methods like toFixed()
or Intl.NumberFormat
, you often end up with a string. If you need to perform mathematical operations on this formatted decimal, you must convert it back to a number. parseFloat()
is commonly used for this, or the unary plus operator +
.
const formattedString = "123.45";
const numberValue = parseFloat(formattedString); // Result: 123.45 (number)
const anotherFormattedString = "5.00";
const numberUsingUnaryPlus = +anotherFormattedString; // Result: 5 (number)
// Be cautious with locale-specific strings if not using Intl.NumberFormat for parsing
const germanFormattedString = "123,45";
const parsedGerman = parseFloat(germanFormattedString); // Result: 123 (parseFloat stops at comma)
Converting string representations of decimals back to number types.
parseFloat()
might not work as expected. For such cases, you might need to manually replace the comma with a dot or use a library that handles locale-aware parsing.