lilywong Posted May 3, 2006 Share Posted May 3, 2006 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] Quote Link to comment Share on other sites More sharing options...
GBS Posted May 3, 2006 Share Posted May 3, 2006 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><head><title>formating & rounding numbers</title></head><body><script>var maxdecimal = 2;var DecimalDelimiter=".";var CommaDelimiter=",";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); integ=formating(integ,CommaDelimiter); } n = minus+integ+DecimalDelimiter+decimal; } else // number is like '.01' or '0.003' { n = minus+"0."+addzeros(n, maxdecimal); } }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,, Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.