Skip to content

Commit c4b64ae

Browse files
authored
fix: use g17 format for doubles (#434)
1 parent 8038ed2 commit c4b64ae

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

CHANGELOG.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## 4.9.0 [unreleased]
22

3+
### Bug Fixes
4+
1. [#408](https://github.com/influxdata/influxdb-client-csharp/issues/408): Conversion of double to string can result in a loss of precision
5+
36
### Others
47
1. [#433](https://github.com/influxdata/influxdb-client-csharp/pull/433): Add `.NET 7.0` to supported `.NET` versions
58

@@ -712,7 +715,7 @@ This release also uses new version of InfluxDB OSS API definitions - [oss.yml](h
712715
713716
### Dependencies
714717
[#283](https://github.com/influxdata/influxdb-client-csharp/pull/283): Update dependencies:
715-
718+
716719
#### Build:
717720
- RestSharp to 107.3.0
718721
- CsvHelper to 27.2.1
@@ -811,7 +814,7 @@ This release introduces a support for new InfluxDB OSS API definitions - [oss.ym
811814
- `Check` <- `CheckDiscriminator` <- `CheckBase`
812815
- `NotificationEndpoint` <- `NotificationEndpointDiscriminator` <- `NotificationEndpointBase`
813816
- `NotificationRule` <- `NotificationRuleDiscriminator` <- `NNotificationRuleBase`
814-
- Flux AST literals extends the AST `Expression` object
817+
- Flux AST literals extends the AST `Expression` object
815818

816819
### Deprecates
817820
- `AuthorizationsApi.CreateAuthorizationAsync(Authorization)`: instead use `AuthorizationsApi.CreateAuthorizationAsync(AuthorizationPostRequest)`
@@ -831,7 +834,7 @@ This release introduces a support for new InfluxDB OSS API definitions - [oss.ym
831834
- Microsoft.Extensions.ObjectPool to 5.0.7
832835

833836
### Documentation
834-
1. [#213](https://github.com/influxdata/influxdb-client-csharp/pull/213): API documentation is deploy to [GitHub Pages](https://influxdata.github.io/influxdb-client-csharp/api/InfluxDB.Client.html)
837+
1. [#213](https://github.com/influxdata/influxdb-client-csharp/pull/213): API documentation is deploy to [GitHub Pages](https://influxdata.github.io/influxdb-client-csharp/api/InfluxDB.Client.html)
835838
836839
## 1.19.0 [2021-06-04]
837840

@@ -951,7 +954,7 @@ This release introduces a support for new InfluxDB OSS API definitions - [oss.ym
951954
1. [#102](https://github.com/influxdata/influxdb-client-csharp/pull/102): Added WriteApiAsync for asynchronous write without batching
952955
953956
### Bug Fixes
954-
1. [#106](https://github.com/influxdata/influxdb-client-csharp/pull/106): Fixed serialization of `\n`, `\r` and `\t` to Line Protocol, `=` is valid sign for measurement name
957+
1. [#106](https://github.com/influxdata/influxdb-client-csharp/pull/106): Fixed serialization of `\n`, `\r` and `\t` to Line Protocol, `=` is valid sign for measurement name
955958
1. [#108](https://github.com/influxdata/influxdb-client-csharp/issues/108): Replaced useless .ContinueWith in Api by direct call
956959
957960
## 1.9.0 [2020-06-19]
@@ -967,8 +970,8 @@ This release introduces a support for new InfluxDB OSS API definitions - [oss.ym
967970
1. [#104](https://github.com/influxdata/influxdb-client-csharp/pull/104): Upgraded InfluxDB 1.7 to 1.8
968971
969972
### Bug Fixes
970-
1. [#100](https://github.com/influxdata/influxdb-client-csharp/pull/100): Thread-safety disposing of clients
971-
1. [#101](https://github.com/influxdata/influxdb-client-csharp/pull/101/): Use Trace output when disposing WriteApi
973+
1. [#100](https://github.com/influxdata/influxdb-client-csharp/pull/100): Thread-safety disposing of clients
974+
1. [#101](https://github.com/influxdata/influxdb-client-csharp/pull/101/): Use Trace output when disposing WriteApi
972975
973976
## 1.8.0 [2020-05-15]
974977

@@ -1064,7 +1067,7 @@ This release introduces a support for new InfluxDB OSS API definitions - [oss.ym
10641067
## 1.0.0 [2019-08-23]
10651068

10661069
### Features
1067-
1. [#29](https://github.com/influxdata/influxdb-client-csharp/issues/29): Added support for gzip compression of query response and write body
1070+
1. [#29](https://github.com/influxdata/influxdb-client-csharp/issues/29): Added support for gzip compression of query response and write body
10681071
10691072
### Bug Fixes
10701073
1. [#27](https://github.com/influxdata/influxdb-client-csharp/issues/27): The org parameter takes either the ID or Name interchangeably

Client.Test/MeasurementMapperTest.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public void ColumnWithoutName()
5757

5858
var lineProtocol = _mapper.ToPoint(poco, WritePrecision.S).ToLineProtocol();
5959

60-
Assert.AreEqual("poco,tag=tag\\ val value=15.444,ValueWithEmptyName=25,ValueWithoutDefaultName=20i 864000",
60+
Assert.AreEqual(
61+
"poco,tag=tag\\ val value=15.444000000000001,ValueWithEmptyName=25,ValueWithoutDefaultName=20i 864000",
6162
lineProtocol);
6263
}
6364

@@ -111,7 +112,8 @@ public void MeasurementProperty()
111112

112113
var lineProtocol = _mapper.ToPoint(poco, WritePrecision.S).ToLineProtocol();
113114

114-
Assert.AreEqual("poco,tag=tag\\ val value=15.444,ValueWithEmptyName=25,ValueWithoutDefaultName=20i 864000",
115+
Assert.AreEqual(
116+
"poco,tag=tag\\ val value=15.444000000000001,ValueWithEmptyName=25,ValueWithoutDefaultName=20i 864000",
115117
lineProtocol);
116118
}
117119

Client.Test/PointDataTest.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,24 @@ public void FieldTypes()
149149

150150
var expected =
151151
"h2o,location=europe boolean=false,byte=9i,decimal=25.6,double=250.69,float=35,integer=7i,long=1i," +
152-
"point=13.3,sbyte=12i,short=8i,string=\"string value\",uint=11u,ulong=10u,ushort=13u";
152+
"point=13.300000000000001,sbyte=12i,short=8i,string=\"string value\",uint=11u,ulong=10u,ushort=13u";
153+
154+
Assert.AreEqual(expected, point.ToLineProtocol());
155+
}
156+
157+
[Test]
158+
public void DoubleFormat()
159+
{
160+
var point = PointData.Measurement("sensor")
161+
.Field("double", 250.69D)
162+
.Field("double15", 15.333333333333333D)
163+
.Field("double16", 16.3333333333333333D)
164+
.Field("double17", 17.33333333333333333D)
165+
.Field("example", 459.29587181322927);
166+
167+
var expected =
168+
"sensor double=250.69,double15=15.333333333333332,double16=16.333333333333332," +
169+
"double17=17.333333333333332,example=459.29587181322927";
153170

154171
Assert.AreEqual(expected, point.ToLineProtocol());
155172
}

Client/Writes/PointData.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,15 @@ private bool AppendFields(StringBuilder sb)
470470
EscapeKey(sb, key);
471471
sb.Append('=');
472472

473-
if (value is double || value is float)
473+
if (value is float)
474474
{
475475
sb.Append(((IConvertible)value).ToString(CultureInfo.InvariantCulture));
476476
}
477+
else if (value is double)
478+
{
479+
var valueStr = ((double)value).ToString("G17", CultureInfo.InvariantCulture);
480+
sb.Append((IConvertible)valueStr);
481+
}
477482
else if (value is uint || value is ulong || value is ushort)
478483
{
479484
sb.Append(((IConvertible)value).ToString(CultureInfo.InvariantCulture));

0 commit comments

Comments
 (0)