Solved

sprintf equivalent

Posted on 1997-06-05
4
3,279 Views
Last Modified: 2011-04-14
is there a built-in javascript function that accomplishes:
      
      sprintf("%.02f", number )
0
Comment
Question by:johnmac
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
garik earned 50 total points
Comment Utility
Nope. Can't even call Java method to accomplish it.
0
 

Author Comment

by:johnmac
Comment Utility
is there anyway to accomplish this specific case
which is to format a real number?

0
 
LVL 3

Expert Comment

by:garik
Comment Utility
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
 

Author Comment

by:johnmac
Comment Utility
thanx garik!

johnmac
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now