Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
07bbaaa3
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
07bbaaa3
编写于
4月 27, 2011
作者:
D
Diego Carrion
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added a :prefix option to number_to_human_size
上级
e190569c
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
17 addition
and
3 deletion
+17
-3
actionpack/lib/action_view/helpers/number_helper.rb
actionpack/lib/action_view/helpers/number_helper.rb
+6
-3
actionpack/test/template/number_helper_test.rb
actionpack/test/template/number_helper_test.rb
+11
-0
未找到文件。
actionpack/lib/action_view/helpers/number_helper.rb
浏览文件 @
07bbaaa3
...
...
@@ -304,6 +304,7 @@ def number_with_precision(number, options = {})
# * <tt>:separator</tt> - Sets the separator between the fractional and integer digits (defaults to ".").
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to "").
# * <tt>:strip_insignificant_zeros</tt> - If +true+ removes insignificant zeros after the decimal separator (defaults to +true+)
# * <tt>:prefix</tt> - If +:si+ formats the number using the SI prefix (defaults to :binary)
# ==== Examples
# number_to_human_size(123) # => 123 Bytes
# number_to_human_size(1234) # => 1.21 KB
...
...
@@ -341,15 +342,17 @@ def number_to_human_size(number, options = {})
options
[
:strip_insignificant_zeros
]
=
true
if
not
options
.
key?
(
:strip_insignificant_zeros
)
storage_units_format
=
I18n
.
translate
(
:'number.human.storage_units.format'
,
:locale
=>
options
[
:locale
],
:raise
=>
true
)
base
=
options
[
:prefix
]
==
:si
?
1000
:
1024
if
number
.
to_i
<
1024
if
number
.
to_i
<
base
unit
=
I18n
.
translate
(
:'number.human.storage_units.units.byte'
,
:locale
=>
options
[
:locale
],
:count
=>
number
.
to_i
,
:raise
=>
true
)
storage_units_format
.
gsub
(
/%n/
,
number
.
to_i
.
to_s
).
gsub
(
/%u/
,
unit
).
html_safe
else
max_exp
=
STORAGE_UNITS
.
size
-
1
exponent
=
(
Math
.
log
(
number
)
/
Math
.
log
(
1024
)).
to_i
# Convert to base 1024
exponent
=
(
Math
.
log
(
number
)
/
Math
.
log
(
base
)).
to_i
# Convert to base
exponent
=
max_exp
if
exponent
>
max_exp
# we need this to avoid overflow for the highest unit
number
/=
1024
**
exponent
number
/=
base
**
exponent
unit_key
=
STORAGE_UNITS
[
exponent
]
unit
=
I18n
.
translate
(
:"number.human.storage_units.units.
#{
unit_key
}
"
,
:locale
=>
options
[
:locale
],
:count
=>
number
,
:raise
=>
true
)
...
...
actionpack/test/template/number_helper_test.rb
浏览文件 @
07bbaaa3
...
...
@@ -172,6 +172,17 @@ def test_number_to_human_size
assert_equal
'10 Bytes'
,
number_to_human_size
(
10
)
end
def
test_number_to_human_size_with_si_prefix
assert_equal
'3 Bytes'
,
number_to_human_size
(
3.14159265
,
:prefix
=>
:si
)
assert_equal
'123 Bytes'
,
number_to_human_size
(
123.0
,
:prefix
=>
:si
)
assert_equal
'123 Bytes'
,
number_to_human_size
(
123
,
:prefix
=>
:si
)
assert_equal
'1.23 KB'
,
number_to_human_size
(
1234
,
:prefix
=>
:si
)
assert_equal
'12.3 KB'
,
number_to_human_size
(
12345
,
:prefix
=>
:si
)
assert_equal
'1.23 MB'
,
number_to_human_size
(
1234567
,
:prefix
=>
:si
)
assert_equal
'1.23 GB'
,
number_to_human_size
(
1234567890
,
:prefix
=>
:si
)
assert_equal
'1.23 TB'
,
number_to_human_size
(
1234567890123
,
:prefix
=>
:si
)
end
def
test_number_to_human_size_with_options_hash
assert_equal
'1.2 MB'
,
number_to_human_size
(
1234567
,
:precision
=>
2
)
assert_equal
'3 Bytes'
,
number_to_human_size
(
3.14159265
,
:precision
=>
4
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录