How to format a number into a specified length in nodejs


i have a value 12.3456789, and i need result like 12.34.

I tried toPrecision() method and got following error.

Object 3.0 has no method 'toPrecision' 

Tried code is,

var ERA = stat['stat_group']['earned_run_average']  
statsHash['earned_run_average'] = ERA.toPrecision(4);
Problem courtesy of: God


For javascript you can try,

if ERA is of type string first convert it to number ERA = 1 * ERA, then

statsHash['earned_run_average'] = 1 * ERA.toFixed(2);

hope this helps.

Solution courtesy of: shakib


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.