fix(rules): enlarge precisions of floats to maximum 17 decimal places.

When printing floats to strings, we have to define a small decimal
limits to avoid print a too long and "inaccurate" float number:

```
2> float_to_binary(0.3).
<<"2.99999999999999988898e-01">>
```

This fix sets precision of floats to 17 digits after the decimal point.

This precision is larger than precision of most `double` data type used by
databases(14 digits for mysql and 15 digits for pgsql).
This commit is contained in:
Shawn 2022-03-18 15:42:01 +08:00
parent 72e37dd144
commit 81ae2be760
1 changed files with 3 additions and 3 deletions

View File

@ -60,8 +60,8 @@
]}). ]}).
-define(EX_PLACE_HOLDER, "(\\$\\{[a-zA-Z0-9\\._]+\\})"). -define(EX_PLACE_HOLDER, "(\\$\\{[a-zA-Z0-9\\._]+\\})").
-define(EX_WITHE_CHARS, "\\s"). %% Space and CRLF -define(EX_WITHE_CHARS, "\\s"). %% Space and CRLF
-define(FLOAT_PRECISION, 17).
-type(uri_string() :: iodata()). -type(uri_string() :: iodata()).
@ -336,12 +336,12 @@ bool(Bool) -> error({invalid_boolean, Bool}).
number_to_binary(Int) when is_integer(Int) -> number_to_binary(Int) when is_integer(Int) ->
integer_to_binary(Int); integer_to_binary(Int);
number_to_binary(Float) when is_float(Float) -> number_to_binary(Float) when is_float(Float) ->
float_to_binary(Float, [{decimals, 10}, compact]). float_to_binary(Float, [{decimals, ?FLOAT_PRECISION}, compact]).
number_to_list(Int) when is_integer(Int) -> number_to_list(Int) when is_integer(Int) ->
integer_to_list(Int); integer_to_list(Int);
number_to_list(Float) when is_float(Float) -> number_to_list(Float) when is_float(Float) ->
float_to_list(Float, [{decimals, 10}, compact]). float_to_list(Float, [{decimals, ?FLOAT_PRECISION}, compact]).
parse_nested(Attr) -> parse_nested(Attr) ->
case string:split(Attr, <<".">>, all) of case string:split(Attr, <<".">>, all) of