How to Format Currency to 2 Decimals in TypeScript

Format currency to 2 decimals in TypeScript: Command Prompt output of Intl.NumberFormat with USD

You can format currency to 2 decimals in TypeScript with Intl.NumberFormat: pass style: ‘currency’ and currency: ‘USD’, and you get a string like $1,234.50 with the symbol, commas and two decimals: Every output on this page came from a real run with TypeScript 7.0.2 and Node.js v22.22.2, strict mode on. Format currency to 2 decimals … Read more >>

pip install -r requirements.txt: Install, Create and Fix Errors

pip install from requirements txt: Command Prompt installing packages into a virtual environment

Running pip install from a requirements txt file takes one command: activate your virtual environment in the project folder, then run pip install -r requirements.txt. pip reads every line and installs those packages and versions: Every command here really ran on Python 3.12.5 in Windows Command Prompt, installing two small packages from PyPI into a … Read more >>

How to Convert String to Float with 2 Decimal Places in Python

Python convert string to float with 2 decimal places: Command Prompt output of round(float(text), 2)

The quickest way to convert string to float with 2 decimal places in Python is round(float(text), 2). float() parses the text and round() keeps two decimal places. If you need the result as text with both digits, use an f-string instead: Every output below comes from my own Command Prompt runs on Python 3.12.5 and … Read more >>

How to Find the Last Occurrence of a Substring in Python

Python find last occurrence of substring: Command Prompt output of rfind on an order log

To find the last occurrence of a substring in Python, call str.rfind(sub). It scans from the right and returns the index where the last match starts, or -1 when the substring isn’t there: All the indexes quoted on this page come straight from Command Prompt runs on Python 3.12.5, using an order log, a receipt … Read more >>

How to Parse a String in Python

Parse a string in Python: Command Prompt output of an order line split into fields and converted to numbers

To parse a string in Python, break it into pieces with split(), clean each piece with strip(), then convert the pieces you need with int() or float(). For patterns, use the re module: Every script below ran in Command Prompt on Python 3.12.5. Save each one as a .py file and run it with python … Read more >>

How to Make a Directory in Python (os and pathlib)

Make a directory in Python: Command Prompt output of pathlib mkdir creating nested folders

Need to make a directory in Python? Use os.mkdir(“name”) for a single folder, or Path(“a/b/c”).mkdir(parents=True, exist_ok=True) to create nested folders without errors if they already exist: Each folder in these examples was created for real by Python 3.12.5 on Windows, and the screenshots show the Command Prompt output. I’ll cover os.mkdir(), os.makedirs(), pathlib, dated and … Read more >>

How to Convert Number to Boolean in TypeScript

Convert number to boolean in TypeScript: Command Prompt output of Boolean(), !! and n !== 0 for 0, -0, NaN and Infinity

The quickest way to convert number to boolean in TypeScript is Boolean(n) or !!n. Both return false for 0, -0 and NaN, and true for every other number, including negatives and Infinity: I ran each snippet below with TypeScript 7.0.2 and Node.js v22.22.2 in strict mode, so the console output you see is real. We’ll … Read more >>

The require Keyword in TypeScript (import = require, TS2591, ESM)

require in TypeScript: Command Prompt output of a CommonJS file requiring an ES module in Node.js 22

The require keyword in TypeScript is really Node.js’s CommonJS require() function: it loads a module synchronously and returns its exports. In a .ts file it needs Node’s types, and it returns any, so prefer import x = require() or plain import: Every example here ran on TypeScript 7.0.2 and Node.js v22.22.2, compiled in strict mode. … Read more >>

How to Get String Between 2 Characters in TypeScript

Get string between 2 characters in TypeScript: Command Prompt output of matchAll pulling every value between brackets

You can get string between 2 characters in TypeScript with indexOf() and slice(): find the start character, search for the end character from just after it, and slice the text in between. Here’s the core of it: Nothing below is typed from memory: I saved each example as a .ts file, compiled it in strict … Read more >>

How to Split a String by Dot in TypeScript (With Examples)

Split string by dot in TypeScript: Command Prompt output of splitting file names into name and extension

To split a string by dot in TypeScript, call split(“.”). It returns a string[] of the pieces between each period. Pass the plain string “.”, not /./, because an unescaped regex dot matches every character: I ran all of this code through TypeScript 7.0.2 and Node.js v22.22.2 with strict mode on, and the outputs below … Read more >>