SVicente's Blog

Follow me and you'll know what I like to do

Javascript – formatar números (Brasil, pt-BR)

Criei o seguinte código:

var _DECIMAL_POINT=','; // modify according to regional settings: . or ,

// this function was created by myself (svicente99@yahoo.com)
// date: 2011, Apr.15th
// ----------------------------------------------------------
function fmt_number_BR(v) {

// primeiro retira caracteres invalidos da string "v"
var VALIDOS="0123456789.";
var vl = "", posDec, aux;
var vInt, vDec;

for (var i=0; i =0)
vl += v.substring(i,i+1);
}
posDec = vl.indexOf('.');
if( posDec >= 0 )
{
vInt = vl.substring(0, posDec);
vDec = _DECIMAL_POINT + vl.substring(posDec+1, vl.length);
}
else{
vInt = vl;
vDec = "";
}
v = "";
n = vInt.length;
if( posDec==0 ) vInt = "0";
j = 1;
do{
v = vInt.substr(n-1,1) + v;
if (j%3==0 && n>1) {
v='.'+v; j = 0;
}
n--; j++;
} while(n>0);
return( v + vDec );
}

function show_format(vl) {
document.write("" + vl + " ==> " + fmt_number_BR(vl) + "
");
}

show_format('24998930114.02323333');
show_format('2499832343120');
show_format('2.023333');
show_format('999');
show_format('0.15');
show_format('1000');
show_format('10');
show_format('0');
show_format('200,001');
show_format('2390435091.000');
show_format('390435091.000');
show_format('12,345.67');
show_format('.1');
show_format('.1349830');
show_format('234.1349830');

Para testar um nº próprio, digite e tecle [TAB]: 

 


Se quiser, teste online.

Deixe seus comentários. Use à vontade; agradeço por citar meus créditos.

April 16, 2011 Posted by | Web Development | , , , , | Leave a comment

Protected: Curriculum Vitae

This content is password protected. To view it please enter your password below:

July 21, 2009 Posted by | Web Development | Enter your password to view comments.

WordPress x Blogger

I decided to post this comparison after a co-worker asked me which would be a good tool to make a blog. In despite of I am using WP now, I have started with Blogger. I am heavy-user and fan of Google products… but, no doubts, WordPress is more complete, easier and user-friendly (“gostoso”) to work.

Follow below a graphical comparison (in another great Google tool: Google Trends) to show how is world tendency to use blog.

A graphical comparison between WordPress and Blogger

A graphical comparison between WordPress and Blogger

—- WordPress
—- Blogger

If you want to continue deeping inside this subject, don’t leave to read this blog post:

http://blog.uolhost.com.br/2009/04/14/porque-usar-o-wordpress-como-plataforma-de-blog/

July 9, 2009 Posted by | Web Development | | Leave a comment

Javascript code – attach event to checkboxes

<html>
<head>
<title>How to dynamically attach a checkbox event click</title>
<script type=”text/javascript”>

// c&p from http://ejohn.org/projects/flexible-javascript-events/
function addEvent(obj, type, fn)
{
if (obj.attachEvent)
{
obj[‘e’ + type + fn] = fn;
obj[type + fn] = function(){obj[‘e’ + type + fn](window.event);}
obj.attachEvent(‘on’ + type, obj[type + fn]);
}
else
obj.addEventListener(type, fn, false);
}
function removeEvent( obj, type, fn ) {
if( obj.detachEvent ) {
obj.detachEvent( ‘on’+type, obj[type+fn] );
obj[type+fn] = null;
} else
obj.removeEventListener( type, fn, false );
}

// Function below is based on an example get from
// http://bytes.com/topic/javascript/answers/725305-please-help-javascript-dynamic-checkboxes-events-question

function fCheckBox(e)
{
var id = e.target.id;
var checked = document.getElementById(id).checked;
if (checked == true)
{
// item was checked.  save new site assignment for this user
alert(“Checkbox #id=”+id+” was clicked and checked.”);
}
else
{
// item was unchecked.  remove this site assignment for this user
alert(“Checkbox #id=”+id+” was clicked and unchecked.”);
}
}

function addEvents2CheckBox() {
for(var i=0; i<document.form.length; i++) {
addEvent(document.form.elements[i], “click”, fCheckBox);
}
}

</script>
</head>

<body onload=”addEvents2CheckBox()”>

<form name=”form” method=”post” action=””>
<input type=”checkbox” id=”chk1″>Check #1<br>
<input type=”checkbox” id=”chk2″>Check #2<br>
<input type=”checkbox” id=”chk3″>Check #3<br>
<input type=”checkbox” id=”chk4″>Check #4<br>
<input type=”checkbox” id=”chk5″>Check #5<br>
</form>
</body>
</html>

May 30, 2009 Posted by | Web Development | | 2 Comments