473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Reg Expression for currency

I need a regular expression to format U.S. currency as 1,215.25 (commas
and decimals)...if it is not already in that format. It should also
allow a '-' sign as the first character for negative numbers. I have
looked in the FAQ and through this group, plus the net and cannot seem
to find the right example.

Any pointers or solutions appreciated. Thanks. I found the below
posted in this group:

<script type="text/javascript">
var RgX = /^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$/

function NewComma(Q) { Q = String(Q)
return (Q == (Q=Q.replace(RgX, "$1$2,$3"))) ? Q : NewComma(Q)
}

function setFormat(elm){
var frmCurr= NewComma(elm);
alert(frmCurr);
}

setFormat(121525);
</script>

May 20 '06 #1
3 1991
johkar wrote:
I need a regular expression to format U.S. currency as 1,215.25 (commas
and decimals)...if it is not already in that format. It should also
allow a '-' sign as the first character for negative numbers. I have
looked in the FAQ and through this group, plus the net and cannot seem
to find the right example.


function format(c)
{
if (typeof c != "string")
c = new String(c);
c = parseFloat(c.split(",").join("").
replace(/^\$/, "")).toFixed(2).toString();
var t = /(\d)(\d{3})([\.\,])/;
for (;;)
{
o = c;
c = c.replace(t, "$1,$2$3");
if (o == c)
break;
}
return c;
}
alert(format("$1,456,789.12"));
alert(format("$1,456,789.129"));
alert(format(-167));
alert(format(20.3));
May 20 '06 #2

TheBagbournes wrote:
johkar wrote:
I need a regular expression to format U.S. currency as 1,215.25 (commas
and decimals)...if it is not already in that format. It should also
allow a '-' sign as the first character for negative numbers. I have
looked in the FAQ and through this group, plus the net and cannot seem
to find the right example.


function format(c)
{
if (typeof c != "string")
c = new String(c);
c = parseFloat(c.split(",").join("").
replace(/^\$/, "")).toFixed(2).toString();
var t = /(\d)(\d{3})([\.\,])/;
for (;;)
{
o = c;
c = c.replace(t, "$1,$2$3");
if (o == c)
break;
}
return c;
}
alert(format("$1,456,789.12"));
alert(format("$1,456,789.129"));
alert(format(-167));
alert(format(20.3));


Perfect, thank you. Still analyzing what every line does.

May 20 '06 #3
TheBagbournes wrote:
johkar wrote:
I need a regular expression to format U.S. currency as 1,215.25 (commas
and decimals)...if it is not already in that format. It should also
allow a '-' sign as the first character for negative numbers. I have
looked in the FAQ and through this group, plus the net and cannot seem
to find the right example.
function format(c)
{
if (typeof c != "string")


Indent (posted) code with spaces, not tabs.
c = new String(c);
No new String object needs to be created for type conversion.
c = parseFloat(c.split(",").join("").
There is no point in split("...").join("") if you can use replace().
There is no point in removing the format first, and then rebuilding
it, if it is already perfect.
replace(/^\$/, "")).toFixed(2).toString();
There is no point in removing a leading `$' but not including it in
the result again. There is no point in toFixed(2).toString(), since
toFixed(...) already returns a string value.
var t = /(\d)(\d{3})([\.\,])/;
Neither the dot nor the comma character needs to be escaped in a
character class. In fact, escaping a special character in a character
class could have the opposite meaning (it does not have in ECMAScript).
for (;;)
{
o = c;
c = c.replace(t, "$1,$2$3");
if (o == c)
break;
}
return c;
}


At least:

function format(n)
{
if (typeof n != "string") n = String(n);

if (!/^\$?(\d{1,3},)*\d{3}.\d\d$/.test(n))
{
n = parseFloat(n.replace(/,|^\$/g, "")).toFixed(2);

var rx = /(\d)(\d{3})([.,])/;

while (rx.test(n))
{
n = n.replace(rx, "$1,$2$3");
}

n = "$" + n;
}

return n;
}
PointedEars
--
Multiple exclamation marks are a sure sign of a diseased mind.
-- Terry Pratchett
May 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jack | last post by:
Hi, I have posted this problem before. Apprently, the suggestion took care of the problem. However, still I am getting the above error message. I am using a session variable to transfer a value...
2
by: Willing 2 Learn | last post by:
I'm still having trouble getting my program to do arithmetic in cents(keeping all #'s) then convert the answer in a format of dollars & cents. The main program should add, subtract, scalar...
2
by: Marc Jennings | last post by:
Hi there, I have an XML document that I am trying to pull some information from. The structure is similar to the following. > <pricelist> > <date>xxxxx</date> > <vendor>bcbcbc</vendor>...
1
by: Terencetrent | last post by:
I am trying to format a query expression drawn from a dialog box as percent. The original statement to get the value for the query is as follows: New%markup: !! The dialog box looks the...
3
by: J | last post by:
Is there anyway to format a calculated datacolumn? I would like the following column to display as money (formatted as $#,##0.00). Or how about simply displaying the column formatted as a number...
1
by: Sreedhar Vankayala | last post by:
Hi, I have a simple textbox in a form page. - If data not available, then user can enter the text "Not available" or "Contact abc person". - If data is available, then user enters the data...
10
by: Mike9900 | last post by:
Hello, I need a regular expression to match a currency with its symbol, for example Pound66.99 must return 66.99 or Pound(66.99) or Pound-66.99 or -66.99Pound return -66.99 or any other...
10
by: aaronrm | last post by:
I have a real simple cross-tab query that I am trying to sum on as the action but I am getting the "data type mismatch criteria expression" error. About three queries up the food chain from this...
2
by: Gulnarambi | last post by:
I have a form in datasheet view with Total and Currency text boxes, in Currency text box I only can enter values of AZN and USD. Form is based on Table and as criterias information fromUnbound...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.