feat: add rule engine function float2str/2
This commit is contained in:
parent
54a6505984
commit
005fce63d1
|
@ -39,6 +39,7 @@
|
|||
bool/1,
|
||||
int/1,
|
||||
float/1,
|
||||
float2str/2,
|
||||
map/1,
|
||||
utf8_bin/1,
|
||||
utf8_str/1,
|
||||
|
@ -258,6 +259,9 @@ float(Num) when is_number(Num) -> erlang:float(Num);
|
|||
float(Data) ->
|
||||
error({invalid_number, Data}).
|
||||
|
||||
float2str(Float, Precision) when is_float(Float) and is_integer(Precision) ->
|
||||
float_to_binary(Float, [{decimals, Precision}, compact]).
|
||||
|
||||
map(Bin) when is_binary(Bin) ->
|
||||
case emqx_json:decode(Bin, [return_maps]) of
|
||||
Map = #{} -> Map;
|
||||
|
|
|
@ -103,6 +103,7 @@
|
|||
int/1,
|
||||
float/1,
|
||||
float/2,
|
||||
float2str/2,
|
||||
map/1,
|
||||
bin2hexstr/1,
|
||||
hexstr2bin/1
|
||||
|
@ -616,6 +617,9 @@ float(Data, Decimals) when Decimals > 0 ->
|
|||
Data1 = ?MODULE:float(Data),
|
||||
list_to_float(float_to_list(Data1, [{decimals, Decimals}])).
|
||||
|
||||
float2str(Float, Precision) ->
|
||||
emqx_plugin_libs_rule:float2str(Float, Precision).
|
||||
|
||||
map(Data) ->
|
||||
emqx_plugin_libs_rule:map(Data).
|
||||
|
||||
|
|
|
@ -169,6 +169,11 @@ t_term_encode(_) ->
|
|||
end,
|
||||
TestData
|
||||
).
|
||||
t_float2str(_) ->
|
||||
?assertEqual(<<"20.2">>, emqx_rule_funcs:float2str(20.2, 1)),
|
||||
?assertEqual(<<"20.2">>, emqx_rule_funcs:float2str(20.2, 10)),
|
||||
?assertEqual(<<"20.199999999999999">>, emqx_rule_funcs:float2str(20.2, 15)),
|
||||
?assertEqual(<<"20.1999999999999993">>, emqx_rule_funcs:float2str(20.2, 16)).
|
||||
|
||||
t_hexstr2bin(_) ->
|
||||
?assertEqual(<<6, 54, 79>>, emqx_rule_funcs:hexstr2bin(<<"6364f">>)),
|
||||
|
|
Loading…
Reference in New Issue