Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3312
  • Last Modified:

sprintf equivalent

is there a built-in javascript function that accomplishes:
      
      sprintf("%.02f", number )
0
johnmac
Asked:
johnmac
  • 2
  • 2
1 Solution
 
garikCommented:
Nope. Can't even call Java method to accomplish it.
0
 
johnmacAuthor Commented:
is there anyway to accomplish this specific case
which is to format a real number?

0
 
garikCommented:
Quick & dirty, but should work fine - even if the number is in E notation:

function format(val) {
  sign = val < 0 ? "-":"";
  rval = Math.abs(Math.round(val*100));
  str = rval.toString();
  if(rval < 10) str = "0.0"+str;
  else if(rval < 100) str = "0."+str;
  else str = str.substring(0, str.length-2)
     + "." + str.substring(str.length-2, str.length);
  return sign+str;
}

0
 
johnmacAuthor Commented:
thanx garik!

johnmac
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now