Skip to content
BasicsChapter 9 / 47Beginner4 min read

Numbers & Math

JavaScript numbers math ka guide, floating point masla, toFixed, Math.round, random numbers aur Intl.NumberFormat. Roman Urdu mein seekhein.

On this page

1. Numbers aur Math kya hain?

Andar se har number ek 64-bit floating point value hai. Is liye 5 aur 5.0 bilkul same hain. Kuch khaas values bhi hain:

ValueKab milti hai
Infinity1 / 0
-Infinity-1 / 0
NaNGhalat math, jaise "abc" * 2
Number.MAX_SAFE_INTEGER9007199254740991, is se upar precision kharab

2. Kyun zaroori hai?

Prices, discounts, ratings, pagination, sab numbers hain. Agar floating point ka masla na pata ho to 0.1 + 0.2 ka jawab 0.30000000000000004 dekh kar hairani hoti hai aur bill mein ghalat paise aa jate hain.

Bina samjheSamajh kar
Rupees decimals mein jodnaPaise integers mein rakhna, aakhir mein divide
Number ko khud format karnaIntl.NumberFormat se currency format
Random ke liye ajeeb formulasMath.floor aur Math.random ka standard pattern
Bari numbers mein zeros ginna1_000_000 jaise numeric separators

3. Kahan use hota hai?

Real useTool
Price ko 2 decimal dikhanatoFixed(2) ya Intl.NumberFormat
Rating round karnaMath.round()
Total pagesMath.ceil(total / perPage)
OTP ya diceMath.random() + Math.floor()
Sab se bari valueMath.max(...values)
Number checkNumber.isInteger(), Number.isNaN()

4. Kaise kaam karta hai?

Computer numbers ko binary mein rakhta hai. 0.1 jaisi decimals binary mein exactly nahi likhi ja saktin, is liye chhota sa farq reh jata hai.

0.1 + 0.2 ka safar
0.1 binary mein, thora sa approximate
0.2 binary mein, thora sa approximate
Dono jode, chhote errors jud gaye
0.30000000000000004

5. Code example

Floating point aur safe comparison:

js
console.log(0.1 + 0.2);
console.log(0.1 + 0.2 === 0.3);

const close = Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON;
console.log(close);

const pricesInPaisa = [1050, 2099, 350];
const totalPaisa = pricesInPaisa.reduce((sum, p) => sum + p, 0);
console.log(totalPaisa / 100);

Output: 0.30000000000000004, false, true, 34.99.

Math methods:

js
console.log(Math.round(4.5));
console.log(Math.floor(4.9));
console.log(Math.ceil(4.1));
console.log(Math.trunc(-4.7));
console.log(Math.max(3, 9, 1));
console.log(Math.abs(-12));
console.log(2 ** 10);

const dice = Math.floor(Math.random() * 6) + 1;
console.log(dice);

Output: 5, 4, 5, -4, 9, 12, 1024, aur 1 se 6 tak koi random number.

Formatting:

js
const salary = 1_250_000;

console.log((19.987).toFixed(2));

const pkr = new Intl.NumberFormat("en-PK", {
  style: "currency",
  currency: "PKR",
});
console.log(pkr.format(salary));

toFixed(2) string "19.99" deta hai. Intl.NumberFormat locale ke hisaab se currency aur commas laga deta hai.

Code kaise kaam kar raha hai?

CheezMatlab
Number.EPSILONDo numbers ke "taqreeban barabar" hone ka chhota sa margin
Math.floor / Math.ceilNeeche / upar wale integer tak
Math.truncDecimal hissa kaat deta hai
Math.random()0 se 1 tak (1 shamil nahi) random decimal
1_250_000Numeric separator, sirf parhne mein asaani
toFixed(2)2 decimals wali string

Interview tip

"0.1 + 0.2 !== 0.3 kyun?" Jawab: JavaScript IEEE 754 double precision floats use karti hai. Binary mein 0.1 aur 0.2 exact nahi, is liye chhota rounding error aata hai. Yeh sirf JavaScript nahi, zyada tar languages mein hota hai.

6. Common mistakes

Galati: toFixed ke result ko number samajhna

Why? toFixed string return karta hai, phir us mein plus lagane se text jud jata hai.

Behtar: Calculation numbers mein karein, toFixed ya Intl.NumberFormat sirf dikhane ke liye use karein.

Galati: Decimals ko seedha === se compare karna

Why? Floating point error ki wajah se 0.1 + 0.2 === 0.3 false aata hai.

Behtar: Integers (jaise paisa) mein kaam karein ya Number.EPSILON wala tolerance check use karein.

Galati: Math.random ko security ke liye use karna

Why? Math.random cryptographically secure nahi, is ke numbers predict kiye ja sakte hain.

Behtar: Tokens ya passwords ke liye crypto.getRandomValues ya crypto.randomUUID use karein.

7. Pehle aur baad mein kya seekhein?

Final quick revision

PointDetail
Ek hi number typeIntegers aur decimals dono, 64-bit float
Floating point0.1 + 0.2 exact 0.3 nahi
PaiseIntegers mein hisaab karein
Roundinground, floor, ceil, trunc
Random intMath.floor(Math.random() * n) + 1
FormattingIntl.NumberFormat, toFixed string deta hai