#### Archived

This topic is now archived and is closed to further replies.

# Detect 1000 separator

## Recommended Posts

Anyone know how to detect 1000 separator in javascript. for example, user key in 123000, it will become 123,000. Thanks.

for example, when i key in the first input as 10000, it should show 10,000 on the second input. Thanks thanks.

[code]<script>
function computeField(input)
{
var intCount = input.value;
loan.convert.value = intCount;
}
</script>

<form name="loan">
Original Key in:<INPUT onChange=computeField(this) size=15 name=price>
with 1000 separator output:<INPUT size=15 name=convert>
</form>[/code]

##### Share on other sites
Hi,,

That one was not easy to script,... erf,... a combination of 2 scripts in 1,, & made few bugs fixes from the 2 versions,...
[code]
<html>
<title>formating & rounding numbers</title>
<body>

<script>
var maxdecimal = 2;
var DecimalDelimiter=".";

function rounding(number, maxdecimal)
{
if (isNaN(number)) // not a number, we skip the function
{
document.getElementById('result').value="NaN";
return false;
}

var integ='';
var decimal='';
var minus='';

if (number.lastIndexOf("-") == 0) // found a negative number
{
minus='-';
number=FormatClean(number); // clean the number ('-15' gives '15' / '-15.6' gives '15.6')
}

if (maxdecimal == 0)
{
var n = Math.round(number); // none maxdecimal set, so we round the number,
document.getElementById('result').value=n.toString();
}
else
{
var p10 = Math.pow(10, maxdecimal);
var n = Math.round(number * p10); // we round the number, keeping the decimals
n = n.toString();
var point = n.length - maxdecimal;

if (point >= 0)
{
decimal=n.substring(point);
if (point == 0) // number is like '.2366' or '0.2366'
{
integ="0";
}
else // number is like '1.2366' / '12354'
{
integ=n.substring(0,point);
}
n = minus+integ+DecimalDelimiter+decimal;
}
else // number is like '.01' or '0.003'
{
}
}
document.getElementById('result').value=n;
}

function FormatClean(number) // removing ponctuation in the number,, ex: -
{
var sVal='';
var nVal = number.length;
var sChar='';

for(i=0;i<nVal;i++)
{
sChar = number.charAt(i);
nChar = sChar.charCodeAt(0);
if ((nChar >=48) && (nChar <=57))
{
sVal += number.charAt(i);
}
else if (nChar ==46) // ouch,,, we must keep the '.' !!
{
sVal += number.charAt(i);
}
}
return sVal;
}

function addzeros(number, maxdecimal) // reformat the numbers like '0.005' (ex: 0.0459, with maxdecimal = 2, gives 0.05)
{
if (isNaN(number)) return "";
var strings = number.toString();
while (strings.length < maxdecimal) strings = "0" + strings;
return strings;
}

function formating(number,CommaDelimiter) // making the '1,000,000' format
{
number = parseInt(number);
var snumber = new String(number);
snumber =  snumber.substring(0,snumber.length);
for (var i = 0; i < Math.floor((snumber.length-(1+i))/3); i++)
{
snumber = snumber.substring(0,snumber.length-(4*i+3)) + CommaDelimiter + snumber.substring(snumber.length-(4*i+3));
}
return snumber;
}
</script>

<table border=1 align=center width="450">
<tr>
<td align=left>
Enter a number: <input id="text" type=text value='' onkeyup="rounding(this.value,maxdecimal);" size=30 maxlength=30>
</td>
<td align=left>
Result: <input id="result" type=text value='' size=30 maxlength=30>
</td>
</tr>
</table>

</body>
</html>
[/code]
difficult thing to solve was making it works with a decimal negative number,... lol
& it's certainly not fully optimised,...

Hoping it helps,,

l8tr,,