Dav*_*och 27 language-agnostic code-golf number-formatting human-readable rosetta-stone
基于这个问题:有没有办法将数字转换为友好格式?
挑战 - 更新! (从规范中删除了数百个缩写)
按字符数排序的最短代码,将缩写整数(无小数).
代码应包括完整的程序.
相关范围来自0 - 9,223,372,036,854,775,807(有符号64位整数的上限).
缩写的小数位数为正数.您不需要计算以下内容:( 920535 abbreviated -1 place这可能是类似的0.920535M).
在十位和百地方(数字0-999)应永远缩写为(该号码的缩写57,以1+小数位是5.7dk-这是不必要的,而不是友好的).
记得从零开始一半(23.5四舍五入到24).银行家的舍入是禁止的.
以下是相关的数字缩写:
h = hundred (102)
k = thousand (103)
M = million (106)
G = billion (109)
T = trillion (1012)
P = quadrillion (1015)
E = quintillion (1018)
SAMPLE INPUTS/OUTPUTS(输入可以作为单独的参数传递):
第一个参数将是缩写的整数.第二个是小数位数.
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...
Run Code Online (Sandbox Code Playgroud)
相关问题的原始答案(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;
}
Run Code Online (Sandbox Code Playgroud)
Dav*_*vid 10
((j.&(1&{)":({.%&1e3{:));{&' kMGTPE'@{.)(([:<.1e3^.{.),{:,{.)
Run Code Online (Sandbox Code Playgroud)
输出:
((j.&(1&{)":({.%&1e3{:));{&' kMGTPE'@{.)(([:<.1e3^.{.),{:,{.) 1500 0
?????
?2?k?
?????
((j.&(1&{)":({.%&1e3{:));{&' kMGTPE'@{.)(([:<.1e3^.{.),{:,{.) 987654321987654321 4
????????????
?987.6543?P?
????????????
Run Code Online (Sandbox Code Playgroud)
(输出是"盒装"的原因是因为J不支持由不同类型组成的列表)
解释(从右到左):
(([:<.1000^.{.),{:,{.)
我们创建了一个新的3元素列表,,用于连接([:<.1000^.{.)(第一个参数的<.基础1000日志.我们将它与第二个参数连接,然后是第一个参数.^.{.{:{.
所以第一位之后,我们已经转化说12345 2成1 2 12345
((j.&(1&{)":({.%&1000{:));{&' kMGTPE'@{.)用于;将表达式的两半连接在一起以生成最终输出.
前半部分是((j.&(1&{)":({.%&1000{:))将%最后一个输入数字({:)除以1000(第一个次数).然后":使用输入列表(1&{)中的第二个数字设置精度.
下半部分{&' kMGTPE'@{.- 这使用第一个数字{从0索引的缩写列表中选择()适当的字符.
a=input()
i=0
while a>=1e3:a/=1e3;i+=1
print"%g"%round(a,input())+" kMGTPE"[i]
Run Code Online (Sandbox Code Playgroud)
此版本(75个字符)使用printf,它将打印额外的零并遵循round-to-even规则.
a=input()
i=0
while a>=1e3:a/=1e3;i+=1
print"%%.%df"%input()%a+" kMGTPE"[i]
Run Code Online (Sandbox Code Playgroud)
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}
Run Code Online (Sandbox Code Playgroud)
[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])
Run Code Online (Sandbox Code Playgroud)
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]
}
Run Code Online (Sandbox Code Playgroud)
这也成为99如果更换(''+a)用a,并承诺只传递一个字符串:)
我的第一个代码 - 高尔夫球入场!
标准输入提供的参数: 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];
Run Code Online (Sandbox Code Playgroud)
918.4k
去高尔夫球版(附说明):
( $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
Run Code Online (Sandbox Code Playgroud)