瀏覽代碼

Use the io_lib_format:fwrite_g function to print floats

This is superior to float_to_list as it only prints the meaningful
digits and 1.0 isn't rendered as "1.00000000000000000000e+00".
pull/10/head
Andrew Thompson 13 年之前
父節點
當前提交
9a577df2e0
共有 1 個檔案被更改,包括 19 行新增1 行删除
  1. +19
    -1
      src/lager_trunc_io.erl

+ 19
- 1
src/lager_trunc_io.erl 查看文件

@ -232,7 +232,9 @@ print(Binary, Max) when is_binary(Binary) ->
{["<<", L, ">>"], Len+4};
print(Float, _Max) when is_float(Float) ->
L = float_to_list(Float),
%% use the same function io_lib:format uses to print floats
%% float_to_list is way too verbose.
L = io_lib_format:fwrite_g(Float),
{L, length(L)};
print(Fun, Max) when is_function(Fun) ->
@ -410,4 +412,20 @@ format_test() ->
?assertEqual("[\"foo\",98,97,114]", lists:flatten(format("~10W", [["foo", $b, $a, $r], 10], 50))),
ok.
atom_quoting_test() ->
?assertEqual("hello", lists:flatten(format("~p", [hello], 50))),
?assertEqual("'hello world'", lists:flatten(format("~p", ['hello world'], 50))),
?assertEqual("hello_world", lists:flatten(format("~p", ['hello_world'], 50))),
?assertEqual("'node@127.0.0.1'", lists:flatten(format("~p", ['node@127.0.0.1'], 50))),
?assertEqual("node@nohost", lists:flatten(format("~p", [node@nohost], 50))),
ok.
sane_float_printing_test() ->
?assertEqual("1.0", lists:flatten(format("~p", [1.0], 50))),
?assertEqual("1.23456789", lists:flatten(format("~p", [1.23456789], 50))),
?assertEqual("1.23456789", lists:flatten(format("~p", [1.234567890], 50))),
?assertEqual("0.3333333333333333", lists:flatten(format("~p", [1/3], 50))),
?assertEqual("0.1234567", lists:flatten(format("~p", [0.1234567], 50))),
ok.
-endif.

Loading…
取消
儲存