From 4c0d8bee317768e421018544015daeb63bb39a9b Mon Sep 17 00:00:00 2001 From: anlicheng Date: Thu, 27 Jul 2023 16:32:13 +0800 Subject: [PATCH] fix --- apps/iot/src/influxdb/influx_point.erl | 5 +++-- apps/iot/src/iot_util.erl | 11 +++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/apps/iot/src/influxdb/influx_point.erl b/apps/iot/src/influxdb/influx_point.erl index 7b9b8ec..0bb8ed7 100644 --- a/apps/iot/src/influxdb/influx_point.erl +++ b/apps/iot/src/influxdb/influx_point.erl @@ -38,8 +38,9 @@ normalized(#point{measurement = Name, tags = Tags, fields = Fields, time = Time} field_val(V) when is_integer(V) -> <<(integer_to_binary(V))/binary, "i">>; -field_val(V) when is_number(V) -> - <<(integer_to_binary(V))/binary, "u">>; +field_val(V) when is_float(V) -> + %% 默认按照浮点数表示 + iot_util:float_to_binary(V, 6); field_val(V) when is_binary(V) -> <<$", V/binary, $">>; field_val(V) when is_list(V); is_map(V) -> diff --git a/apps/iot/src/iot_util.erl b/apps/iot/src/iot_util.erl index d0cb7be..4e8f378 100644 --- a/apps/iot/src/iot_util.erl +++ b/apps/iot/src/iot_util.erl @@ -10,7 +10,7 @@ -author("licheng5"). %% API --export([timestamp/0, number_format/2, current_time/0, timestamp_of_seconds/0]). +-export([timestamp/0, number_format/2, current_time/0, timestamp_of_seconds/0, float_to_binary/2]). -export([step/3, chunks/2, rand_bytes/1, uuid/0, md5/1, parse_mapper/1]). -export([json_data/1, json_error/2]). -export([queue_limited_in/3, assert_call/2]). @@ -114,4 +114,11 @@ parse_mapper(Mapper) when is_list(Mapper) -> {ok, F}; false -> error - end. \ No newline at end of file + end. + +-spec float_to_binary(Num :: number(), integer()) -> binary(). +float_to_binary(V, _) when is_integer(V) -> + integer_to_binary(V); +float_to_binary(V, Decimals) when is_float(V), is_integer(Decimals) -> + S = float_to_list(V, [{decimals, Decimals}, compact]), + list_to_binary(S). \ No newline at end of file