Number
- This lesson explains the Number object and its important methods.
Introduction to Numbers in JavaScript
Numbers are one of the most fundamental data types in JavaScript.
JavaScript uses a single number type to represent:Integers (whole numbers)
Floating-point numbers (decimal values)
Examples:
let a = 10;
let b = 25.75;
Both values are treated as Number type in JavaScript.
Number & Math Methods
JavaScript Number Object
JavaScript provides a built-in Number object that allows us to:
Perform numeric operations
Convert values
Handle precision
Validate numeric data
Common Number Methods
Number()
Converts a value into a number.
Convert Values to Number Using Number()
Converts different data types like strings and booleans into numeric values
Number("100"); // 100
Number("10.5"); // 10.5
Number(true); // 1
Number(false); // 0
isNaN()
Checks whether a value is Not a Number.
Check Invalid Numbers Using isNaN()
Determines whether a value is Not a Number (NaN) or a valid number
isNaN("Hello"); // true
isNaN(10); // false
toString()
Converts a number into a string.
Convert Number to String Using toString()
Changes a numeric value into a string format
let num = 50;
num.toString(); // "50"
toFixed()
Formats a number with a fixed number of decimal places.
Format Number with Fixed Decimal Places
Rounds and formats a number to a specified number of decimal digits
let price = 99.4567;
price.toFixed(2); // "99.46"
Important :
toFixed() returns a string, not a number.
JavaScript Math Object
JavaScript provides a built-in Math object for mathematical operations.
The Math object:
Is not a constructor
Is used directly
Contains static methods
Math Object
Provides built-in properties and methods for performing mathematical operations
Math.PI; // 3.141592653589793
- Common Math Methods
Useful Math Functions
Useful Math object methods for calculations and random numbers
// Value of Pi
Math.PI;
// Power of a number (2^3)
Math.pow(2, 3); // 8
// Square root
Math.sqrt(16); // 4
// Maximum and minimum values
Math.max(10, 20, 5); // 20
Math.min(10, 20, 5); // 5
// Random number between 0 and 1
Math.random();
// Random number between 1 and 10
Math.floor(Math.random() * 10) + 1;
Rounding Numbers
(Math.round, Math.ceil, Math.floor)
Why Rounding Is Needed
In real-world applications:
Prices
Marks
Measurements
Calculations
Math.round()
Rounds a number to the nearest integer.
.5 and above → rounds up
.4 and below → rounds down
Rounding Numbers Using Math.round()
Rounds a number to the nearest integer based on decimal value
// Rounds down (below .5)
Math.round(4.4); // 4
// Rounds up (.5 and above)
Math.round(4.5); // 5
Math.round(4.7); // 5
Math.ceil()
Rounds a number upward to the nearest integer.
Always Round Up Using Math.ceil()
Rounds a number up to the next nearest integer
// Always rounds up
Math.ceil(4.1); // 5
Math.ceil(4.9); // 5
Always goes up.
Math.floor()
Rounds a number downward to the nearest integer.
Always Round Down Using Math.floor()
Rounds a number down to the nearest integer
// Always rounds down
Math.floor(4.9); // 4
Math.floor(4.1); // 4
Always goes down.
Comparison of Rounding Methods
Method Behavior Math.round() Nearest value Math.ceil() Always up Math.floor() Always down Use Cases
Math.round() → Average marks
Math.ceil() → Billing, seat allocation
Math.floor() → Age calculation, discounts
Handling Decimal Precision
The Decimal Precision Problem
JavaScript uses floating-point arithmetic, which can cause precision issues.
Floating Point Precision Issue
Shows how decimal calculations in JavaScript can lead to unexpected results
// Decimal precision problem
0.1 + 0.2; // 0.30000000000000004
This is normal behavior in JavaScript.
Why This Happens
JavaScript follows IEEE 754 standard
Some decimal values cannot be represented exactly in binary
Fixing Decimal Precision Issues
Provides solutions to handle floating-point calculation errors
// Problem: Floating point precision issue
let sum = 0.1 + 0.2;
// Solution 1: Using toFixed()
sum.toFixed(2); // "0.30"
Number(sum.toFixed(2)); // convert back to number
// Solution 2: Multiply and Divide Technique
let result = (0.1 * 10 + 0.2 * 10) / 10;
console.log(result); // 0.3
// Solution 3: Using Math.round()
let value = Math.round((0.1 + 0.2) * 100) / 100;
console.log(value); // 0.3
Best Practices for Decimal Precision
Avoid direct comparison of decimals
Always round before displaying
Use toFixed() for UI
Use rounding logic for calculations
Common Beginner Mistakes
Comparing decimal numbers directly
Forgetting toFixed() returns string
Using wrong rounding method
Ignoring precision issues in money calculations