Question

Sur la base de cette question:

Autres conseils

Python 2.x, 78 caractères

a=input()
i=0
while a>=1e3:a/=1e3;i+=1
print"%g"%round(a,input())+" kMGTPE"[i]

Cette version ( 75 caractères ) utilise printf qui imprimera des zéros supplémentaires et suit la règle ronde à même.

a=input()
i=0
while a>=1e3:a/=1e3;i+=1
print"%%.%df"%input()%a+" kMGTPE"[i]

Perl 114 111 104 caractères

Mon premier entrée de code-golf!

Arguments fournis à partir de l'entrée standard: perl fna.pl 918395 1

($n,$d)=@ARGV;
@n=$n=~/./g;
@s=' kMGTPE'=~/./g;
printf"%.".(@n>3?$d:0)."f%s",$n/(10**($#n-$#n%3)),$s[@n/3];

Sortie:

  

918.4k


Version De-golfed (avec explication):

( $number, $dp ) = @ARGV;      # Read in arguments from standard input

@digits = split //, $number;   # Populate array of digits, use this to count
                               # how many digits are present

@suffix = split //, ' kMGTPE'; # Generate suffix array

$number/(10**($#n-$#n%3));     # Divide number by highest multiple of 3

$precision = @n>3 ? $dp : 0;   # Determine number of decimal points to print

sprintf "%.".$precision."f%s", # "%.2f" prints to 2 dp, "%.0f" prints integer
        $number, $suffix[@n/3];# Select appropriate suffix

Javascript 114 caractères

function m(n,d){p=M.pow
d=p(10,d)
i=7
while(i)(s=p(10,i--*3))<=n&&(n=M.round(n*d/s)/d+"kMGTPE"[i])
return n}

En outre 114 - Utilisation de spidermonkey - Entrée sur STDIN

[n,d]=readline().split(' '),x=n.length,p=Math.pow,d=p(10,d)
x-=x%3
print(Math.round(n*d/p(10,x))/d+" kMGTPE"[x/3])

104 - Fonction

function(a,b,c,d){
    c=(''+a).length;
    d=Math.pow;
    b=d(10,b);
    return((a*b/d(10,c-=c%3))+.5|0)/b+' kMGTPE'[c/3]
}

Ce qui est également 99 si vous remplacez le (''+a) avec a et promettent de ne laisser passer que les chaînes:)

dc - 75 caractères

A7 1:U77 2:U71 3:U84 4:U80 5:U69 6:U[3+r1-r]sJ?sddZd3~d0=Jrsp-Ar^ldk/nlp;UP

Utilise Z (nombre de chiffres) %3 pour trouver l'unité. La plupart du code est pour le réglage du tableau de caractères unités, le code réel est de 39 caractères. Les ajuste macro J quand %3 est égal à 0, afin d'éviter l'impression 0.918M dans le 7ème. cas de test. Il ne arrondit pas correctement.

Si vous parlez dc, ne hésitez pas à l'améliorer.

PHP 57 caractères

for($a=num+1;$a>=1;$a=$a/26)$c=chr(--$a%26+65).$c;echo$c;

Haskell, 126 (sans l'importation, il est une fonction qui prend deux arguments):

f n p|l>3=showFFloat (Just p) (c n/c 10^(l-w)) [" kMGTPE"!!f]|True=show n where(f,w)=divMod l 3;c=fromIntegral;l=length$show n

élargi:

import Numeric

doit :: Integer -> Int -> String
doit n p
    | l > 3 = showFFloat (Just p) d [" kMGTPE" !! f]
    | otherwise = show n
    where
    d = (fromIntegral n) / fromIntegral (10^(l-w))
    (f,w) = divMod l 3
    l = length $ show n

Perl 94 Chars

($_,$d)=@ARGV;$l=length;@u=' kMGTPE'=~/./g;printf"%.".($l>3?$d:0)."f$u[$l/3]",$_/10**($l-$l%3)

Utilisation:

perl abbreviator.pl 47475782130 2

Sortie:

47.48G
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top