bobbyhadz blog

I write articles about Web Development and Cloud Computing.

522
Get the Last 2 Digits of a Number in JavaScript

Get the Last 2 Digits of a Number in JavaScript

To get the last 2 digits of a number convert the number to a string and call the `slice()` method on the string, passing it `-2` as a parameter. The `slice` method will return the ...

javascript
avatar

Borislav Hadzhiev

Sat Oct 23 20211 min read

Get the First 2 Digits of a Number in JavaScript

Get the First 2 Digits of a Number in JavaScript

To get the first 2 digits of a number convert the number to a string and call the `slice()` to get the first 2 characters. Then convert the string back to a number to get the first...

javascript
avatar

Borislav Hadzhiev

Sat Oct 23 20211 min read

Get the Last Digit of a Number in JavaScript

Get the Last Digit of a Number in JavaScript

To get the last digit of a number convert the number to a string and call the `slice()` method on the string, passing it `-1` as a parameter. The `slice` method will return the las...

javascript
avatar

Borislav Hadzhiev

Sat Oct 23 20211 min read

Get the second Digit of a Number in JavaScript

Get the second Digit of a Number in JavaScript

To get the second digit of a number convert the number to a string. Access the string at index `1`, using square brackets notation e.g. `String(num)[1]`. Convert the result back to...

javascript
avatar

Borislav Hadzhiev

Sat Oct 23 20211 min read

Get the first Digit of a Number in JavaScript

Get the first Digit of a Number in JavaScript

To get the first digit of a number convert the number to a string. Access the string at index `0`, using square brackets notation e.g. `String(num)[0]`. Convert the result back to ...

javascript
avatar

Borislav Hadzhiev

Sat Oct 23 20211 min read

Get the number of digits after the Decimal in JavaScript

Get the number of digits after the Decimal in JavaScript

To get the number of digits after the decimal check if the number is an integer, if it is, the number of digits is `0`. Otherwise, convert it to a string, and use the `split()` met...

javascript
avatar

Borislav Hadzhiev

Sat Oct 23 20211 min read

Round a Number to 3 Decimal Places in JavaScript

Round a Number to 3 Decimal Places in JavaScript

Use the `toFixed()` method to round a number to 3 decimal places, e.g. `num.toFixed(3)`. The `toFixed` method formats a number to a specified number of decimal places and rounds th...

javascript
avatar

Borislav Hadzhiev

Sat Oct 23 20212 min read

Round a Number to 1 Decimal Place in JavaScript

Round a Number to 1 Decimal Place in JavaScript

Use the `toFixed()` method to round a number to 1 decimal place, e.g. `num.toFixed(1)`. The `toFixed` method formats a number to a specified number of decimal places and rounds the...

javascript
avatar

Borislav Hadzhiev

Sat Oct 23 20212 min read

Add Leading Zeros to a String in JavaScript

Add Leading Zeros to a String in JavaScript

Use the `padStart()` method to add leading zeros to a string. The method allows us to pad the current string with zeros to a specified target length and returns the result.

javascript
avatar

Borislav Hadzhiev

Sat Oct 23 20212 min read

Change getMonth to 2 digits in JavaScript

Change getMonth to 2 digits in JavaScript

To change the `getMonth()` method to 2 digits add `1` to the result from calling `getMonth()` and convert it to a string. Use the `padStart()` method to add a leading zero if it's ...

javascript
avatar

Borislav Hadzhiev

Sat Oct 23 20212 min read

Change getDate to 2 digits in JavaScript

Change getDate to 2 digits in JavaScript

To change the `getDate()` method to 2 digits convert the result from calling `getDate()` to a string. Use the `padStart()` method to add a leading zero if it's necessary. The `padS...

javascript
avatar

Borislav Hadzhiev

Sat Oct 23 20212 min read

Change getSeconds to 2 digits in JavaScript

Change getSeconds to 2 digits in JavaScript

To change the `getSeconds()` method to 2 digits convert the result from calling `getSeconds()` to a string. Use the `padStart()` method to add a leading zero if it's necessary.

javascript
avatar

Borislav Hadzhiev

Sat Oct 23 20212 min read

Change getHours to 12 Hour Format in JavaScript

Change getHours to 12 Hour Format in JavaScript

To change the `getHours()` method to a 12 hour format use the modulo `%` operator to get the remainder of dividing a call to the `getHours()` method and `12`. If the remainder is g...

javascript
avatar

Borislav Hadzhiev

Sat Oct 23 20212 min read

Change getHours to 2 digits in JavaScript

Change getHours to 2 digits in JavaScript

To change the `getHours()` method to 2 digits convert the result from calling `getHours()` to a string. Use the `padStart()` method to add a leading zero if it's necessary.

javascript
avatar

Borislav Hadzhiev

Sat Oct 23 20212 min read

Get Minutes with Leading Zero in JavaScript

Get Minutes with Leading Zero in JavaScript

To get the minutes of a date with a leading zero use the `getMinutes()` method to get the minutes and convert the result to a string. THen use the `padStart()` method to add a lead...

javascript
avatar

Borislav Hadzhiev

Sat Oct 23 20212 min read

Get Month and Date in 2 Digit Format in JavaScript

Get Month and Date in 2 Digit Format in JavaScript

To add leading zeros to a date, convert the month and the day of the date to strings and call the `padStart()` method on them. The `padStart` method allows us to add leading zeros ...

javascript
avatar

Borislav Hadzhiev

Sat Oct 23 20212 min read

How to Pad a number with Leading Zeros in JavaScript

How to Pad a number with Leading Zeros in JavaScript

To pad a number with leading zeros convert the number to a string and call the `padStart()` method. The `padStart` method allows you to add leading zeros to the start of the string...

javascript
avatar

Borislav Hadzhiev

Sat Oct 23 20211 min read

Remove Trailing Zeros from a Number in JavaScript

Remove Trailing Zeros from a Number in JavaScript

To remove the trailing zeros from a number, pass the number to the `parseFloat()` function. The `parseFloat` function parses the provided value returning a floating point number, w...

javascript
avatar

Borislav Hadzhiev

Sat Oct 23 20211 min read

Round a Number to 2 Decimal Places in JavaScript

Round a Number to 2 Decimal Places in JavaScript

Use the `toFixed()` method to round a number to 2 decimal places, e.g. `num.toFixed(2)`. The `toFixed` method formats a number to a specified number of decimal places and rounds th...

javascript
avatar

Borislav Hadzhiev

Sat Oct 23 20212 min read

Format a number to 2 Decimal places in JavaScript

Format a number to 2 Decimal places in JavaScript

Use the `toFixed()` method to format a number to 2 decimal places, e.g. `num.toFixed(2)`. The `toFixed` method takes a parameter, representing how many digits should appear after t...

javascript
avatar

Borislav Hadzhiev

Sat Oct 23 20211 min read

How to Parse Float with 2 Decimal places in JavaScript

How to Parse Float with 2 Decimal places in JavaScript

To parse a float with 2 decimal places, pass the number as a parameter to the `parseFloat()` function and call the `toFixed(2)` method on the result. The `toFixed` method will form...

javascript
avatar

Borislav Hadzhiev

Fri Oct 22 20211 min read

author avatar

Borislav Hadzhiev

Web Developer

SUPPORT ME : )

buy me a coffee
Buy Me A Coffee