高尔夫代码:友好号码的缩写 [英] Code-Golf: Friendly Number Abbreviator
问题描述
基于此问题: 挑战-已更新! (已从规范中删除了数百个缩写)
按字符计数的最短代码,它将缩写为整数(无小数).
The shortest code by character count that will abbreviate an integer (no decimals).
代码应包含完整程序.
相关范围从0 - 9,223,372,036,854,775,807
(带符号的64位整数的上限)开始.
Relevant range is from 0 - 9,223,372,036,854,775,807
(the upper limit for signed 64 bit integer).
缩写的小数位数为正. 您将不需要计算以下内容:920535 abbreviated -1 place
(类似于0.920535M
).
The number of decimal places for abbreviation will be positive. You will not need to calculate the following: 920535 abbreviated -1 place
(which would be something like 0.920535M
).
千万不要缩写成几百位数(0-999
)(57
到1+
小数位的缩写是5.7dk
-不必要和不友好).
Numbers in the tens and hundreds place (0-999
) should never be abbreviated (the abbreviation for the number 57
to 1+
decimal places is 5.7dk
- it is unneccessary and not friendly).
请记住从零舍入一半(23.5舍入为24).银行家的舍入是固定的.
Remember to round half away from zero (23.5 gets rounded to 24). Banker's rounding is verboten.
以下是相关的数字缩写:
Here are the relevant number abbreviations:
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
)
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
)
样品输入/输出(输入 可以作为单独的参数传递):
SAMPLE INPUTS/OUTPUTS (inputs can be passed as separate arguments):
第一个参数是要缩写的整数.第二个是小数位数.
First argument will be the integer to abbreviate. The second is the number of decimal places.
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,不遵循规范):
Original answer from related question (JavaScript, does not follow spec):
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, 61 63 65 characters
((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不支持包含不同类型的列表)
(The reason the output is "boxed" like that is because J doesn't support a list consisting of varying types)
说明(从右到左):
(([:<.1000^.{.),{:,{.)
使用,
联接([:<.1000^.{.)
(第一个参数{.
的下层<.
以1000为底的对数^.
),我们创建了一个新的3元素列表.我们将其与第二个参数{.
.
We make a new 3-element list, using ,
to join ([:<.1000^.{.)
(the floored <.
base 1000 log ^.
of the first param {.
. We join it with the second param {:
and then the first param {.
.
因此,在第一行之后,我们将12345 2
转换为1 2 12345
So after the first bit, we've transformed say 12345 2
into 1 2 12345
((j.&(1&{)":({.%&1000{:));{&' kMGTPE'@{.)
使用;
将表达式的两半结合在一个盒子中以产生最终输出.
((j.&(1&{)":({.%&1000{:));{&' kMGTPE'@{.)
uses ;
to join the two halves of the expression together in a box to produce the final output.
前半部分是((j.&(1&{)":({.%&1000{:))
,它将最后一个输入数字({:
)除以(%
),即第一次输入的次数.然后,它使用输入列表中的第二个数字(1&{
)设置精度":
.
The first half is ((j.&(1&{)":({.%&1000{:))
which divides (%
) the last input number ({:
) by 1000, the first number of times. Then it sets the precision ":
using the second number in the input list (1&{
).
后半部{&' kMGTPE'@{.
-它使用第一个数字从0索引的缩写列表中选择({
)适当的字符.
The second half {&' kMGTPE'@{.
- this uses the first number to select ({
) the appropriate character from the 0-indexed list of abbreviations.
这篇关于高尔夫代码:友好号码的缩写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!