Permalink
Browse files

Merge pull request #10 from basho/adt-better-float-printing

Use the io_lib_format:fwrite_g function to print floats
  • Loading branch information...
2 parents 3babca1 + 9a577df commit dadd0cf53f5abaf25d6eed801b4031f8753f982d @Vagabond Vagabond committed Aug 29, 2011
Showing with 19 additions and 1 deletion.
  1. +19 −1 src/lager_trunc_io.erl
View
20 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.

0 comments on commit dadd0cf

Please sign in to comment.