Network_ninja Posted November 2, 2011 Share Posted November 2, 2011 Is anyone have a handy code that will format a number? both decimal and thousand separator? eg. value = 1000.2532 result -> 1,000.25 tnx in advance Quote Link to comment https://forums.phpfreaks.com/topic/250271-javascript-format/ Share on other sites More sharing options...
sunfighter Posted November 2, 2011 Share Posted November 2, 2011 From Javascript Source: <!-- TWO STEPS TO INSTALL CURRENCY FORMAT: 1. Copy the coding into the HEAD of your HTML document 2. Add the last code into the BODY of your HTML document --> <!-- STEP ONE: Paste this code into the HEAD of your HTML document --> <HEAD> <SCRIPT LANGUAGE="JavaScript"> <!-- Original: Cyanide_7 (leo7278@hotmail.com) --> <!-- Web Site: http://www7.ewebcity.com/cyanide7 --> <!-- This script and many more are available free online at --> <!-- The JavaScript Source!! http://javascript.internet.com --> <!-- Begin function formatCurrency(num) { num = num.toString().replace(/\$|\,/g,''); if(isNaN(num)) num = "0"; sign = (num == (num = Math.abs(num))); num = Math.floor(num*100+0.50000000001); cents = num%100; num = Math.floor(num/100).toString(); if(cents<10) cents = "0" + cents; for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3)); return (((sign)?'':'-') + '$' + num + '.' + cents); } // End --> </script> </HEAD> <!-- STEP TWO: Copy this code into the BODY of your HTML document --> <BODY> <center> <form name=currencyform> Enter a number then click the button: <input type=text name=input size=10 value="1000434.23"> <input type=button value="Convert" onclick="this.form.input.value=formatCurrency(this.form.input.value);"> <br><br> or enter a number and click another field: <input type=text name=input2 size=10 value="1000434.23" onBlur="this.value=formatCurrency(this.value);"> </form> </center> <p><center> <font face="arial, helvetica" size="-2">Free JavaScripts provided<br> by <a href="http://javascriptsource.com">The JavaScript Source</a></font> </center><p> <!-- Script Size: 1.47 KB --> Quote Link to comment https://forums.phpfreaks.com/topic/250271-javascript-format/#findComment-1284315 Share on other sites More sharing options...
Network_ninja Posted November 3, 2011 Author Share Posted November 3, 2011 tnx for the code. il try it out later. Quote Link to comment https://forums.phpfreaks.com/topic/250271-javascript-format/#findComment-1284455 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.