Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk2 min

Convert a Number to a String in TypeScript

Use String(value) for a straightforward conversion, toString(radix) for another base, or a template literal to embed a number in text. Learn the edge cases and precision limit.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use String(value) for a clear general-purpose conversion, value.toString(radix) when you need a particular number base, or a template literal such as `${value}` to insert the number into surrounding text. These are JavaScript runtime operations, so they work the same way in TypeScript.

Choose the conversion that fits

Use Example Best fit
String(value) String(count) Explicit, general-purpose conversion to text.
value.toString(radix) count.toString(16) Converting a number by itself, especially when you need a non-decimal base.
Template literal `${count}` Embedding a number in a larger string.

For example:

const count: number = 42;
const asString: string = String(count);       // "42"
const alsoString: string = count.toString();   // "42"
const message: string = `Count: ${count}`;     // "Count: 42"
const hex: string = count.toString(16);        // "2a"

Convert to a different number base

toString() without an argument uses decimal. Pass an integer radix from 2 through 36 to represent the number in another base: for example, value.toString(2) for binary or value.toString(16) for hexadecimal. Passing a radix outside that range, or one that is not an integer, throws a RangeError. See the Number.prototype.toString() reference.

For negative numbers, the minus sign is preserved in other radixes. The resulting binary text is not a two’s-complement representation; it is the number’s sign and magnitude expressed using the requested base.

Know what the resulting text represents

Decimal notation can be exponential

When converting in base 10, JavaScript can use scientific notation for numbers with a magnitude of at least 1021 or below 10-6. If your output must follow a particular display convention, verify the resulting representation rather than assuming every value will appear as ordinary decimal digits.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Special numeric values have text forms

Both 0 and -0 convert to "0". NaN, Infinity, and -Infinity convert to their corresponding text forms.

Converting does not recover lost integer precision

A JavaScript Number cannot exactly represent every integer. If a large integer was rounded when it became a Number, converting it to a string gives text for that rounded value—not the original digits. For example, distinct large integer literals can compare equal after Number representation, as noted in the Number conversion reference.

Rank #2
TypeScript Programming Language - Software Engineer & Coder T-Shirt
  • TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
  • TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem

When exact integer digits beyond the safe range matter, use BigInt where appropriate and avoid passing the value through Number. The BigInt reference describes the separate integer type.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Text conversion is not display formatting

String(value), toString(), and template interpolation convert the numeric value to text; they do not, by themselves, provide currency symbols, grouped digits, or locale-specific decimal separators. Treat those as formatting requirements rather than basic conversion, and choose a formatting API suited to the output you need.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.