473,386 Members | 1,846 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,386 software developers and data experts.

currency format

I found this javascript on javascript.internet.com and it returns a
decimal value i.e. 88,999.45 and if I didn't want a decimal value
returned what changes should I make. Given the example above I'd like to
return 8,899,945.

Any help is appreciated. Mike
<form>
Enter Value:
<input type=text name=test length=15
onKeyPress="return(currencyFormat(this,',','.',eve nt))">
</form>

function currencyFormat(fld, milSep, decSep, e)
{
var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789';
var aux = aux2 = '';
var whichCode = (window.Event) ? e.which : e.keyCode;
if (whichCode == 13) return true; // Enter
key = String.fromCharCode(whichCode); // Get key value from key code
if (strCheck.indexOf(key) == -1) return false; // Not a valid key
len = fld.value.length;
for(i = 0; i < len; i++)
if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep))
break;
aux = '';
for(; i < len; i++)
if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux +=
fld.value.charAt(i);
aux += key;
len = aux.length;
if (len == 0) fld.value = '';
if (len == 1) fld.value = '0'+ decSep + '0' + aux;
if (len == 2) fld.value = '0'+ decSep + aux;
if (len > 2) {
aux2 = '';
for (j = 0, i = len - 3; i >= 0; i--) {
if (j == 3) {
aux2 += milSep;
j = 0;
}
aux2 += aux.charAt(i);
j++;
}
fld.value = '';
len2 = aux2.length;
for (i = len2 - 1; i >= 0; i--)
fld.value += aux2.charAt(i);
fld.value += decSep + aux.substr(len - 2, len);
}
return false;
}

Jul 20 '05 #1
7 11348
Yep
Michael Hill <hi****@ram.lmtas.lmco.com> wrote in message news:<3F***************@ram.lmtas.lmco.com>...
Given the example above I'd like to
return 8,899,945.


<script type="text/javascript">
function f(field){
if(/^\d+$/.test(field.value))
field.value=field.value.split("").reverse().join(" ").
replace(/(\d{3})/g,"$1,").
replace(/,$/,"").
split("").reverse().join("");
}
</script>
<input type="text" onblur="f(this)">

(after a conception by Boniface Lau, IIRC)
Jul 20 '05 #2
y-*******@em-lyon.com (Yep) writes:
replace(/(\d{3})/g,"$1,").
replace(/,$/,"").


These two lines can be shortened to one as
replace(/(\d{3})\B/g,"$1,").
The "\B" matches a zero-width non-word-boundary, so it will not add a
comma after the end of the string.

Why do I find it annoying that it is necessary to reverse the string,
when I know that regular languages are indifferent to direction?

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
Lasse Reichstein Nielsen <lr*@hotpop.com> writes:
Why do I find it annoying that it is necessary to reverse the string,
when I know that regular languages are indifferent to direction?


Hmm, as an (almost) completely unrelated side note ...
The regular expressions of Javascript (or Perl) are actually more
powerful that "real" regular expressions, in the sense that they
recognize languages that are not regular (or even recursive).
So my annoyance is misplaced.

Bonus points for telling which strings (containing only the digit 1)
this RegExp recognizes: /^(?!(11+)\1+$)/

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4
Yep
Lasse Reichstein Nielsen <lr*@hotpop.com> wrote in message news:<sm**********@hotpop.com>...
Lasse Reichstein Nielsen <lr*@hotpop.com> writes:
Why do I find it annoying that it is necessary to reverse the string,
when I know that regular languages are indifferent to direction?

Well in our case you're not obliged to reverse the string, you could
also use something like
/(?=(\d{3})+\b)/g
and remove the leading comma with another replace. In languages
supporting lookbehind operators (not js) that'd be even easier.
Bonus points for telling which strings (containing only the digit 1)
this RegExp recognizes: /^(?!(11+)\1+$)/


Your chain could be only of the form 1+, so the first (11+) will match
all the 1 of the string (being greedy), that is, all the string. Then
the backtracking starts; the first possible match is when the regexp
has backtracked half part of it (division by 2); we'd have (11+) ==
\1. If it cannot succeed, then it continues backtracking until the
initial number of one can be "divided by three", i.e. we'd have 111
for the (11+) part, (111)(111) for the \1+. If it cannot succeed, then
it goes on...

To conclude, the regexp will try have have n parts of equal number of
"1", with n belonging to [1; N]. If the regexp cannot succeed, this
means it has not been able to make the correct repartion; put it
another way, the number cannot be divided by any of [1; N-1]. So your
regexp will match any prime number of "1".

I'm not a regexp expert, but the way I understand how regexps work,
that'd be quite heavy a calculation just to get prime numbers
(especially since the \1+ would imply far more states than actually
described in my explanation).
Regards,
Yep.
Jul 20 '05 #5
y-*******@em-lyon.com (Yep) writes:
Lasse Reichstein Nielsen <lr*@hotpop.com> wrote in message news:<sm**********@hotpop.com>...
Lasse Reichstein Nielsen <lr*@hotpop.com> writes:
Why do I find it annoying that it is necessary to reverse the string,
when I know that regular languages are indifferent to direction?

Well in our case you're not obliged to reverse the string, you could
also use something like
/(?=(\d{3})+\b)/g
and remove the leading comma with another replace. In languages
supporting lookbehind operators (not js) that'd be even easier.


Smart move! It can even get better:
string.replace(/(?=(\d{3})+$)\B/g,",")
replaces the non-boundary with a positive multiple of three number of
digits after it.
So your regexp will match any prime number of "1".


Bonus points gøs to you.
The point of that exercise was not to show how inefficient it could get,
but to point out that it was possible at all.

The theory of regular languages tells us that they can be recognized
by a finite state machine. Regular expressions have been compiled into
such state machines (in, e.g., lexical analysers like Lex or Flex,
that work in linear time without backtracking (building the state
machine is hard work, though)).

The addition of the back reference (\1) changes the computational
power of the "regular expressions". They can now recognize a language
(like primes in unary notation) that is not regular. It is recursive,
meaning that it requires the equivalent of a Turing machine to
recognize ... or a general purpose computer. It can no longer be
implemented in finite space and without backtracking.

In practice, nobody complains, because we already have the entire
string in memory.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6
Yep
Lasse Reichstein Nielsen <lr*@hotpop.com> wrote in message news:<4r**********@hotpop.com>...
string.replace(/(?=(\d{3})+$)\B/g,",")
replaces the non-boundary with a positive multiple of three number of
digits after it.
Seems perfect, I'm just unfamiliar with \B, although I do use nonword
boundaries every day :-)
Bonus points gøs to you.
I'd rather get a beer. Thanks anyway.
The addition of the back reference (\1) changes the computational
power of the "regular expressions". They can now recognize a language
(like primes in unary notation) that is not regular.


Never going to bed without my ECMA copy, I can tell you there's a GCD
regexp (or iregexp should I say) that will please you in the regexp
algorithms part (you may already have read it).
Cheers,
Yep 'simple machine'.
Jul 20 '05 #7
JRS: In article <4r**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lr*@hotpop.com>
posted at Fri, 15 Aug 2003 16:12:13 :-

Smart move! It can even get better:
string.replace(/(?=(\d{3})+$)\B/g,",")
replaces the non-boundary with a positive multiple of three number of
digits after it.


Such solutions seem to need a warning that only browsers later than ...
can handle them.

Various perhaps more compatible ways to insert commas are in my
<URL:http://www.merlyn.demon.co.uk/js-maths.htm#OutComma>. The
shortest, albeit heavily criticised, of those is :-

function RComma(S) { S = String(S)
var RgX = /^(.*\s)?([-+\u00A3\u20AC]?\d+)(\d{3}\b)/
return S == (S=S.replace(RgX, "$1$2,$3")) ? S : RComma(S) }

which will comma-ise the integer parts of all numbers in S, in at least
MSIE4 & presumably later. A "g" at the end of RgX is optional. A
number must be preceded by whitespace or nothing at all.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #8

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

Similar topics

1
by: Bill Stanard | last post by:
I have had no success using the format function as follows (the two lines of code run one after the other): 'displays a running total of lblAmtdue.Caption 'contents in txtTotal.Text...
3
by: John L | last post by:
Well, another basic type of question...amazing how much I've forgotten - never was a VB guru, but. I can see I'll have to pick up a couple VB5 instruction books - any suggestions on a couple good...
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: Dalan | last post by:
This should not be an issue, but it is. I'm sure that someone knows what little piece of code is needed too persuade Access 97 to include a currency format for labels (Avery, mailing type). Have...
3
by: news.shaw.net | last post by:
I have a subform that contains a currency field. If I tab my way to the field, everything works as desired, a number can be entered, and when I leave the field it is rendered as a currency...
2
by: Bob Dydd | last post by:
Hi Everbody Question Changing the regional settings ie. from UK to US AUS etc, Access retains the original currency format that the application was created in. So if an mdb that is written...
4
by: Gerry Abbott | last post by:
Hi All. How can i put a zero decimal for my currency into my format statement, == & ", for " & & " days. Cost " & Format(,"Currency") which is the data source for a textbox on a report
7
by: zlf | last post by:
1¡¢Here is a number. double nu = 123 2¡¢Format it with 3¡¢The result is "$123" I want to ask how to deformat "$123" to 123 Thx (Currency symbol is not restricted to '$')
2
by: crferguson | last post by:
Hello all! I'm having the oddest issue trying to format a numeric string as currency without decimals. For instance... strSalary = "120000.56" strSalary = Format(strSalary, "$#,##0") 'this...
2
by: Ian | last post by:
I have an Access 2000 database written in the UK, this database has many fields set to Currency, I now want to move this database over to a user in the USA but my Currency fields still show the UK...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.