Numbers – A Weblog of Priyank Maniar https://priyank.rocks Articles on web development & mobile app development Sun, 10 Jul 2022 03:07:15 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 JavaScript: Working with Numbers https://priyank.rocks/javascript-numbers/ Tue, 19 Mar 2019 17:06:28 +0000 http://priyank.rocks/?p=402 Do you know how to get highest number that’s available in Javascript (Safely)?


1
Number.MAX_VALUE

Good Reads

  1. https://medium.com/swlh/how-to-round-to-a-certain-number-of-decimal-places-in-javascript-ed74c471c1b8
  2. https://stackoverflow.com/a/48764436/6275860

Advise

When performing any arithmetic operations such as addition. Always check if the candidate number variables are not Nan. Otherwise, the result becomes a NaN.

1
2
3
parseFloatReplaceNaNByZero(item) {
    return (!isNaN(parseFloat(item)) ? parseFloat(item) : 0)
},

What is JavaScript equivalent for number_format in PHP?


1
2
3
4
5
6
7
8
9
// Lodash to rescue

// Works exactly as we expect. Output is a typeof number.
_.round("10.608", 2); // 10.61

// Following returns the output as string. That's one major down side.
parseFloat("10").toFixed(2); // 10.00
parseFloat("10.60").toFixed(2); // 10.60
parseFloat("10.605").toFixed(2); // 10.61

]]>