2010-04-21 13 views
20

Proprio come la reputazione Stackoverlow arrotondamento, spero di fare la stessa cosa con la moneta

$ 1.000 => 1k

$ 1.000.000 di => 1m

C'è una biblioteca fuori c'è già questo? (Preferibilmente in jQuery)

+1

Questo al momento non è una domanda accettabile per il codice del golf. vedi http://meta.stackexchange.com/questions/24258 Hai bisogno di un paio di casi di test, per renderlo wiki della comunità e, infine, si dovrebbe accettare la risposta più breve (in termini di dimensioni del codice). I campi da golf a codice singolo sono noiosi, dovresti aprirlo anche ad altre lingue –

+0

David ha creato una versione [codec golf]: http://stackoverflow.com/questions/2692323/code-golf-friendly-number-abbreviator – dmckee

+1

Grazie ragazzi - come puoi vedere sono nuovo di Stack Overflow. Sono eccitato che tutti pensiate che questo sia un problema interessante! – Baloneysammitch

risposta

71

Ecco una semplice funzione di farlo:

function abbrNum(number, decPlaces) { 
    // 2 decimal places => 100, 3 => 1000, etc 
    decPlaces = Math.pow(10,decPlaces); 

    // Enumerate number abbreviations 
    var abbrev = [ "k", "m", "b", "t" ]; 

    // Go through the array backwards, so we do the largest first 
    for (var i=abbrev.length-1; i>=0; i--) { 

     // Convert array index to "1000", "1000000", etc 
     var size = Math.pow(10,(i+1)*3); 

     // If the number is bigger or equal do the abbreviation 
     if(size <= number) { 
      // Here, we multiply by decPlaces, round, and then divide by decPlaces. 
      // This gives us nice rounding to a particular decimal place. 
      number = Math.round(number*decPlaces/size)/decPlaces; 

      // Handle special case where we round up to the next abbreviation 
      if((number == 1000) && (i < abbrev.length - 1)) { 
       number = 1; 
       i++; 
      } 

      // Add the letter for the abbreviation 
      number += abbrev[i]; 

      // We are done... stop 
      break; 
     } 
    } 

    return number; 
} 

Uscite:

abbrNum(12 , 1)   => 12 
abbrNum(0 , 2)   => 0 
abbrNum(1234 , 0)  => 1k 
abbrNum(34567 , 2)  => 34.57k 
abbrNum(918395 , 1)  => 918.4k 
abbrNum(2134124 , 2)  => 2.13m 
abbrNum(47475782130 , 2) => 47.48b 

Dimostrazione: http://jsfiddle.net/jtbowden/SbqKL/

+1

questo sembra golfy. quanto piccolo possiamo ottenere questa funzione? –

+0

lol, molto più piccolo sono sicuro. –

+0

FONDO IT. Seriamente, mi piacerebbe vederlo in un filo da golf. –

10
var pow=Math.pow, floor=Math.floor, abs=Math.abs, log=Math.log; 

function round(n, precision) { 
    var prec = Math.pow(10, precision); 
    return Math.round(n*prec)/prec; 
} 

function format(n) { 
    var base = floor(log(abs(n))/log(1000)); 
    var suffix = 'kmb'[base-1]; 
    return suffix ? round(n/pow(1000,base),2)+suffix : ''+n; 
} 

Dimostrazione:

> tests = [-1001, -1, 0, 1, 2.5, 999, 1234, 
      1234.5, 1000001, Math.pow(10,9), Math.pow(10,12)] 
> tests.forEach(function(x){ console.log(x,format(x)) }) 

-1001 "-1k" 
-1 "-1" 
0 "0" 
1 "1" 
2.5 "2.5" 
999 "999" 
1234 "1.23k" 
1234.5 "1.23k" 
1000001 "1m" 
1000000000 "1b" 
1000000000000 "1000000000000"