language-agnostic - Code-Golf:友好数字缩写

标签 language-agnostic code-golf rosetta-stone number-formatting human-readable

锁定。这个问题及其答案是 locked 因为这个问题是题外话但具有历史意义。它目前不接受新的答案或互动。








基于这个问题:Is there a way to round numbers into a friendly format?

挑战 - 更新! (从规范中删除了数百个缩写)

按字符计数的最短代码将缩写一个整数(无小数)。

代码应该包括完整的程序。

相关范围来自 0 - 9,223,372,036,854,775,807(有符号 64 位整数的上限)。

缩写的小数位数将为正数。您不需要计算以下内容: 920535 abbreviated -1 place (类似于 0.920535M )。

十位和百位的数字 ( 0-999 ) 永远不应该缩写(数字 571+ 小数位的缩写是 5.7dk - 这是不必要的,也不友好)。

请记住从零开始舍入一半(23.5 舍入为 24)。银行家的四舍五入是禁止的。

以下是相关的数字缩写:
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^.{.),{:,{.)
我们创建一个新的 3 元素列表,使用 ,加入([:<.1000^.{.) (第一个参数 <. 的地板 ^. base 1000 log {. 。我们将它与第二个参数 {: 和第一个参数 {. 连接起来。

所以在第一位之后,我们已经转换成 12345 2进入 1 2 12345((j.&(1&{)":({.%&1000{:));{&' kMGTPE'@{.)用途 ;将表达式的两半连接在一个框中以产生最终输出。

上半年是((j.&(1&{)":({.%&1000{:))它将 ( % ) 最后一个输入数字 ( {: ) 除以 1000,即第一次。然后设置精度 ":使用输入列表中的第二个数字 ( 1&{ )。

下半场{&' kMGTPE'@{. - 这使用第一个数字从 0 索引的缩写列表中选择 ( { ) 适当的字符。

关于language-agnostic - Code-Golf:友好数字缩写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2692323/

相关文章:

language-agnostic - Code Golf : Lights out

regex - 子序列搜索

algorithm - 支持快速下一个高元素和下一个低元素的数据结构是什么?

c# - 异步调用 void 方法的最简洁方法

language-agnostic - 高尔夫代码:1x1黑色像素

language-agnostic - Code Golf : The wave

javascript - 除 JavaScript 之外的任何其他语言在大括号开始位置(同一行和下一行)之间是否有区别?

language-agnostic - 是(宽,高)还是(高,宽)?

r - 如何打开函数名称完成?

language-agnostic - Code Golf : Print the entire "12 Days of Christmas" song in the fewest lines of code