代码高尔夫:友好号码缩写
题
基于这个问题: 有没有办法将数字变成友好的格式?
挑战 - 更新! (从规格中删除了数百个缩写)
最短的字符计数代码将缩写一个整数(无小数)。
代码应包括完整程序。
相关范围来自 0 - 9,223,372,036,854,775,807
(签名64位整数的上限)。
缩写的小数位数将为正。 您无需计算以下内容: 920535 abbreviated -1 place
(就像 0.920535M
).
数十个地方的数字(0-999
) 应该 绝不 缩写(数字的缩写 57
到 1+
小数点是 5.7dk
- 这是不仔细的,不友好的)。
切记距离零半(23.5被四舍五入到24)。银行家的圆形是verboten。
这是相关数字缩写:
h = hundred (10
2
)
k = thousand (10
3
)
M = million (10
6
)
G = billion (10
9
)
T = trillion (10
12
)
P = quadrillion (10
15
)
E = quintillion (10
18
)
样品输入/输出 (输入 能够 作为单独的参数通过):
第一个论点将是缩写的整数。第二个是小数点的数量。
12 1 => 12 // tens and hundreds places are never rounded
1500 2 => 1.5k
1500 0 => 2k // look, ma! I round UP at .5
0 2 => 0
1234 0 => 1k
34567 2 => 34.57k
918395 1 => 918.4k
2134124 2 => 2.13M
47475782130 2 => 47.48G
9223372036854775807 3 => 9.223E
// ect...
相关问题的原始答案(JavaScript,不遵循规格):
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;
// Add the letter for the abbreviation
number += abbrev[i];
// We are done... stop
break;
}
}
return number;
}
解决方案
J,61 63 65 人物
((j.&(1&{)":({.%&1e3{:));{&' kMGTPE'@{.)(([:<.1e3^.{.),{:,{.)
输出:
((j.&(1&{)":({.%&1e3{:));{&' kMGTPE'@{.)(([:<.1e3^.{.),{:,{.) 1500 0
┌─┬─┐
│2│k│
└─┴─┘
((j.&(1&{)":({.%&1e3{:));{&' kMGTPE'@{.)(([:<.1e3^.{.),{:,{.) 987654321987654321 4
┌────────┬─┐
│987.6543│P│
└────────┴─┘
(输出之所以“盒装”之所以这样,是因为j不支持由不同类型组成的列表)
说明(从右到左):
(([:<.1000^.{.),{:,{.)
我们使用 ,
加入 ([:<.1000^.{.)
(地板 <.
基本1000日志 ^.
第一个参数 {.
. 。我们加入第二个参数 {:
然后是第一个参数 {.
.
因此,在第一个一点之后,我们转换了 12345 2
进入 1 2 12345
((j.&(1&{)":({.%&1000{:));{&' kMGTPE'@{.)
用途 ;
将表达式的两半加在一起,以产生最终输出。
上半场是 ((j.&(1&{)":({.%&1000{:))
分裂(%
)最后一个输入号({:
)到1000,第一个次数。然后设置精度 ":
使用输入列表中的第二个数字(1&{
).
下半场 {&' kMGTPE'@{.
- 这使用第一个数字选择({
)缩写列表中的适当字符。
其他提示
Python 2.x,78个字符
a=input()
i=0
while a>=1e3:a/=1e3;i+=1
print"%g"%round(a,input())+" kMGTPE"[i]
这个版本(75个字符)使用将打印额外的零并遵循往返规则的printf。
a=input()
i=0
while a>=1e3:a/=1e3;i+=1
print"%%.%df"%input()%a+" kMGTPE"[i]
珀尔 114 111 104个字符
我有史以来第一个代码高尔夫条目!
根据标准输入提供的论点: 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];
输出:
918.4k
De-Golfed版本(带有解释):
( $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个字符
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}
也114-使用蜘蛛孔 - 在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-功能
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]
}
如果您更换 (''+a)
和 a
并保证只通过字符串:)
红宝石 - 79 77 75 83个字符
n,d=ARGV
l=n.to_s.length
printf"%.#{l>3?d:0}f%s",n.to_f/10**(l-l%3)," kMGTPE"[l/3]
从命令行参数读取。
74 72 80个字符,在双引号中打印输出
n,d=ARGV
l=n.to_s.length
p"%.#{l>3?d:0}f%s"%[n.to_f/10**(l-l%3)," kMGTPE"[l/3]]
66 74个字符,打印额外的零
n,d=ARGV
l=n.to_s.length
p"%.#{d}f%s"%[n.to_f/10**(l-l%3)," kMGTPE"[l/3]]
基于 这 解决方案和示例代码。
DC -75个字符
A7 1:U77 2:U71 3:U84 4:U80 5:U69 6:U[3+r1-r]sJ?sddZd3~d0=Jrsp-Ar^ldk/nlp;UP
用途 Z
(位数) %3
找到单元。大多数代码用于设置单元字符数组,实际代码为39个字符。这 J
宏调整何时 %3
等于 0
, ,避免打印 0.918M
在第七。测试用例。它没有正确的圆形。
如果你说话 dc
, ,请随时改进它。
PHP 57个字符
for($a=num+1;$a>=1;$a=$a/26)$c=chr(--$a%26+65).$c;echo$c;
Haskell,126(没有导入,这是一个需要两个参数的函数):
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
扩展:
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
佩尔94个炭
($_,$d)=@ARGV;$l=length;@u=' kMGTPE'=~/./g;printf"%.".($l>3?$d:0)."f$u[$l/3]",$_/10**($l-$l%3)
用法:
perl abbreviator.pl 47475782130 2
输出:
47.48G