/    Sign up×
Community /Pin to ProfileBookmark

Please Help, need to convert 1000 to 1,000…

Hello everybody…

I looking for help!!!

What i need is when I enter a number(s) into field if it is longer then x then place a comma.

This is a price field that could accept:

1000; 1,000; 1000.00; 1,000.00
but display only:
1,000

Where can I find some info about this? If I have to search internet, what is the best keyword to use that will describe it?

Please, Please help!

Andre

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@brendandonhueMay 03.2003 — Found it on javascript source.


<SCRIPT LANGUAGE="JavaScript">

<!-- Begin

function commaSplit(srcNumber) {

var txtNumber = '' + srcNumber;

if (isNaN(txtNumber) || txtNumber == "") {

alert("Oops! That does not appear to be a valid number. Please try again.");

fieldName.select();

fieldName.focus();

}

else {

var rxSplit = new RegExp('([0-9])([0-9][0-9][0-9][,.])');

var arrNumber = txtNumber.split('.');

arrNumber[0] += '.';

do {

arrNumber[0] = arrNumber[0].replace(rxSplit, '$1,$2');

} while (rxSplit.test(arrNumber[0]));

if (arrNumber.length > 1) {

return arrNumber.join('');

}

else {

return arrNumber[0].split('.')[0];

}

}

}

// End -->

</script>

<BODY>

<form name="commas">

Input a Number:

<input type=text name="inpNumber" size=20 value="">

<input type=button value="Add Commas" onClick="document.commas.inpNumber.value=commaSplit(document.commas.inpNumber.value);">

</form>
Copy linkTweet thisAlerts:
@CharlesMay 03.2003 — [font=monospace]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

"http://www.w3.org/TR/html4/strict.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<meta name="Content-Script-Type" content="text/javascript">

<script type="text/javascript">

<!--

String.prototype.reverse = function () {return this.split('').reverse().join('')};

Number.prototype.withCommas = function () {

if (isNaN(this)) return 'NaN';

var n = this.toString().split('.');

n[0] = n[0].reverse().match(/d{1,3}/g).join(',').reverse();

return n.join('.');

}

// -->

</script>

<form action="">

<div>

<input type="text" onchange="this.value = Number(this.value).withCommas()">

<div>

</form>[/font]
×

Success!

Help @conexp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...