473,468 Members | 1,319 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1993
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...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.