Formatting Javascript Strings and Numbers to 2 Decimal Places

Updated Version

There’s an updated version of this article here:


We have a number and a string and we want to format both to 2 decimal places:

var num1: any = 213.73145;
var num2: any = '213.73145';


* Round the number.
* @param num The number
* @param precision The precision
export function round(num: number, precision: number = 2) {
return (Math.round(num * 100) / 100).toFixed(precision);
var num1: any = 213.73145;
var num2: any = '213.73145';


Founder of Firefly Semantics Corporation

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

5 Killer chrome devTools features that will change your life

How do we use HOC and Context API to handle states in our React projects?

Custom Directives in Angular

Image showing Angular custom directives

Host Your Own React Site with Firebase and Github Actions

Mizuno Wave Prophecy 4

JavaScript Interview Question: Pair with Target Sum

A Simple Bus Schedule Viewing App using Flutter

Bus Schedule View Widget

How Much Does It Cost To Create An Online Pet Store App

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ole Ersoy

Ole Ersoy

Founder of Firefly Semantics Corporation

More from Medium

Sorting Javascript Objects in Ascending Order by Date

call(), apply() & bind() methods in Javascript

oop in javascript! part1: prototype

Events, Arrow functions, and DOM manipulation in JavaScript