Code-Golf:友好的数字缩写器
基于这个问题: 是 办法将数字舍入为友好的格式?
挑战 - 已更新! (从规范中删除了数百个缩写)
有没有 按字符计数的代码将缩写为整数(无小数)。
代码应包含完整的程序。
相关范围为 0 - 9,223,372,036,854,775,807
(有符号 64 位整数的上限)。
缩写的小数位数将为正数。 您不需要计算以下内容:920535 缩写 -1 place
(类似于 0.920535M
)。
十位和十位中的数字百位 (0-999
) 绝不 应该缩写(数字 57
到 1+
十进制的缩写地方是5.7dk
- 这是不必要的并且不友好)。
请记住从零开始舍入一半(23.5 舍入为 24)。禁止银行家四舍五入。
以下是相关数字缩写:
< br> h = Hundred (10
2
)
k = 千 (10
3
)
M = 百万 (10
6
)
G = 十亿(10
9
)
T = 万亿 (10
12
)
P = 四万亿 (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;
}
Based on this question: Is there a way to round numbers into a friendly format?
THE CHALLENGE - UPDATED! (removed hundreds abbreviation from spec)
The shortest code by character count that will abbreviate an integer (no decimals).
Code should include the full program.
Relevant range is from 0 - 9,223,372,036,854,775,807
(the upper limit for signed 64 bit integer).
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
).
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).
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
)
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...
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
J, 61
6365个字符输出:(
输出像这样“装箱”的原因是因为 J 不支持由不同类型组成的列表)
解释(来自从右到左):
(([:<.1000^.{.),{:,{.)
我们使用
,
创建一个新的 3 元素列表加入([:<.1000^.{.)
(第一个参数的底数<.
以 1000 为底的 log^.
{.
。我们将其与第二个参数{:
和第一个参数{.
连接起来。将
12345 2
转换为1 2 12345
((j.&(1&{)":({.%&1000{:)); {&' kMGTPE'@{.)
使用;
将表达式的两半连接到一个框中以产生最终输出。前半部分是
(( j.&(1&{)":({.%&1000{:))
除 (%
) 最后一个输入数字 ({:) 乘以第一个次数 1000,然后使用输入列表中的第二个数字 (
1&{
) 设置精度":
。后半部分
{&' kMGTPE'@{.
- 使用第一个数字从 0 索引的缩写列表中选择 ({
) 适当的字符。J, 61
6365charactersOutput:
(The reason the output is "boxed" like that is because J doesn't support a list consisting of varying types)
Explanation (from right to left):
(([:<.1000^.{.),{:,{.)
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{.
.So after the first bit, we've transformed say
12345 2
into1 2 12345
((j.&(1&{)":({.%&1000{:));{&' kMGTPE'@{.)
uses;
to join the two halves of the expression together in a box to produce the final output.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&{
).The second half
{&' kMGTPE'@{.
- this uses the first number to select ({
) the appropriate character from the 0-indexed list of abbreviations.Python 2.x,78 个字符
此版本(75 个字符)使用 printf,它将打印额外的零并遵循舍入到偶数规则。
Python 2.x, 78 chars
This version (75 chars) uses printf which will print extra zeros and follows the round-to-even rule.
Perl
114111104 个字符我的第一个代码高尔夫条目!
标准输入提供的参数:
perl fna.pl 918395 1
输出:
脱高尔夫球版本(带解释):
Perl
114111104 charsMy first ever code-golf entry!
Arguments provided from standard input:
perl fna.pl 918395 1
Output:
De-golfed version (with explanation):
Javascript 114 个字符
另外 114 - 使用 Spidermonkey - 在 STDIN 上输入
104 - 函数
如果您将
(''+a)
替换为a
并承诺仅通过,则该函数也将变为 99字符串:)Javascript 114 chars
Also 114 - Using spidermonkey - Input on STDIN
104 - Function
Which also becomes 99 if you replace the
(''+a)
witha
and promise to only pass strings :)Ruby -
79777583 个字符从命令行参数读取。
747280 个字符,在双引号内打印输出6674 个字符,打印额外的零基于 此解决方案以及示例代码。
Ruby -
79777583 charsReads from command line arguments.
747280 chars, prints output within double quotes6674 chars, prints extra zeroesBased on this solution, and the sample code.
dc - 75 个字符
使用
Z
(位数)%3
查找单位。大部分代码是设置单位字符数组,真正的代码是39个字符。当%3
等于0
时,J
宏会进行调整,以避免在第 7 个位置打印0.918M
。测试用例。它不能正确舍入。如果您说
dc
,请随时改进它。dc - 75 chars
Uses
Z
(number of digits)%3
to find the unit. Most of the code is for setting the units character array, the real code is 39 chars. TheJ
macro adjusts when%3
equals0
, to avoid printing0.918M
in the 7th. test case. It doesn't round properly.If you speak
dc
, feel free to improve it.PHP 57 个字符
PHP 57 chars
Haskell,126(没有导入,它是一个带有两个参数的函数):
扩展:
Haskell, 126 (without import, it's a function that takes two arguments):
Expanded:
Perl 94 个字符
用法:
输出:
Perl 94 Chars
Usage:
Output: