Feb 7, 2013

Rounding a number to two decimal places in Javascript / Node JS

Posted Feb 7, 2013
Rounding numbers with decimals in Javascript is not straightforward. Javascript has a utility Math.round but it only rounds to the nearest integers. Some uses toFixed function e.g. (2.55555).toFixed(2) but it doesn't work consistently across all browsers, sometimes it just truncates the value which makes it troublesome for computations.

Creating a simple function for rounding numbers to two decimals in Javascript should be easy:

function roundTo2Decimals(numberToRound) {
  return Math.round(numberToRound * 100) / 100
}

//test
roundTo2Decimals(2.55555555);

If you want to use a library, numeraljs is a good one. It also formats numbers with commas. Very useful.


Feb 1, 2013

How to Get the email address from Twitter Developer API [Rant]

Posted Feb 1, 2013
You can't; sorry to disappoint. As of writing, this is an unresolved issue and many developers are hoping for a positive reply from the Twitter development team: See this discussion and this one for more information. Seems that the Twitter dev team has no plan to support getting the user's email address from Twitter API.


It is unfortunate that this is the case. One big factor of going through the pain of setting up a 3rd party single sign on service is to avoid the process of verifying the user's email address (which takes developer effort). In Twitter's case, you have to do both (set-up authentication AND verify user's email). The same goes for the user who still has to undergo the email verification process. -1 for usability.

I don't know if it's privacy issue that Twitter decided to opt this out, but Facebook and Google, along with other services, are allowing it. Just be transparent and say to the user, this application will know about your email address. What's the big deal, Twitter?