My Opera is closing 3rd of March

Iniciando o Hoje

Começando a avançar

Formatar Número em JavaScript

,

Ta ai abaixo uma forma simples de formatar um número de acordo com a moeda desejada.
<script>
 /**
  * @author: Flåviø Sïlvå
  * @version: 1.0
  * @date: 18/03/2011
  * @contact: http://www.flaviosilva.net
  * 
  * Format a number with grouped thousand
  *
  * @param int decimals;
  * @param char sepDecimals;
  * @param char sepDecimals;
  * @return String;
  */

 Number.prototype.formatNumber = function(decimals, sepDecimals, sepThousand){
  if(decimals==null)decimals=2;  
  if(sepDecimals==null)sepDecimals=',';
  if(sepThousand==null)sepThousand='.';  

  var n = new String(this.toFixed(decimals)).replace('.','').split('');
  n.reverse();
  var fn = new Array();
  var cont = decimals+1;
  for(this.i=0;this.i<n.length;this.i++){
   if(this.i==decimals-1 && n.length>decimals-1){
    fn.unshift(sepDecimals+n[this.i]);
   }else{
    if(cont--==0 && this.i != n.length-1){
     fn.unshift(sepThousand+n[this.i]);
     cont = 2;
    }else fn.unshift(n[this.i]);
   } 
  }
  return fn.join('');
 }
</script>

Como usar? Simples
<script>
 var a = 2501258845456; // Número a ser formatado;

 /*
  A função funciona com os seguintes parametros, o primeiro é a quantidade
  de casas decimais, a segunda e o sinal do separador decimal, e a terceira
  é o sinal do separador de milhar;
 */
 document.write(a.formatNumber(2,',','.'));
 // Escreve: 25.012.588.454,56
</script>

Saiba como subir no PageHankInterrupção na requisão Ajax

Write a comment

New comments have been disabled for this post.

February 2014
S M T W T F S
January 2014March 2014
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28