[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Comma Formatted Javascript

Posted on 2012-08-20
1
Medium Priority
?
522 Views
Last Modified: 2012-08-20
I have some javascript that comma-formats number inputs.  Works great unless the number is a negative, and then it is adding a commas between the - sign and the first number.    My changes haven't made a difference.   Any ideas what I need to add?

function formatNumber(num) {
  num = rev(num);
  num = num.replace(/(\d{3})/g, '$1,');
  num = rev(num);
  if(num.charAt(0) == ',') num = num.substr(1);
  return num;
}

Open in new window

0
Comment
Question by:digitalwise
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 38314602
Try this:

num = num.replace(/(\d{3})(?!-)/g, '$1,');

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
Suggested Courses

834 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