From 2d937a67a8227c01b763b694bbfa355cc03e9d35 Mon Sep 17 00:00:00 2001 From: glopesdev Date: Wed, 22 Feb 2023 22:41:04 +0000 Subject: [PATCH] Increase default max number of significant digits --- Bonsai.System/Bonsai.System.csproj | 2 +- Bonsai.System/IO/CsvWriter.cs | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Bonsai.System/Bonsai.System.csproj b/Bonsai.System/Bonsai.System.csproj index 2eba4d7e5..c31aaf5b5 100644 --- a/Bonsai.System/Bonsai.System.csproj +++ b/Bonsai.System/Bonsai.System.csproj @@ -5,7 +5,7 @@ Bonsai System Library containing reactive infrastructure to interface with the underlying operating system. Bonsai Rx Reactive Extensions IO Serial Port Resources net462;netstandard2.0 - 2.7.0 + 2.7.1 diff --git a/Bonsai.System/IO/CsvWriter.cs b/Bonsai.System/IO/CsvWriter.cs index 9e0347073..70a6b04a6 100644 --- a/Bonsai.System/IO/CsvWriter.cs +++ b/Bonsai.System/IO/CsvWriter.cs @@ -25,6 +25,7 @@ namespace Bonsai.IO public class CsvWriter : CombinatorExpressionBuilder { string delimiter; + static readonly Expression InvariantCulture = Expression.Constant(CultureInfo.InvariantCulture); static readonly MethodInfo stringJoinMethod = typeof(string).GetMethods().Single(m => m.Name == nameof(string.Join) && m.GetParameters().Length == 2 && m.GetParameters()[1].ParameterType == typeof(string[])); @@ -184,23 +185,31 @@ static bool IsNullable(Type type) static Expression GetDataString(Expression expression) { if (expression.Type == typeof(string)) return expression; + if (expression.Type == typeof(float)) + { + return Expression.Call(expression, nameof(ToString), null, Expression.Constant("G9"), InvariantCulture); + } + else if (expression.Type == typeof(double)) + { + return Expression.Call(expression, nameof(ToString), null, Expression.Constant("G17"), InvariantCulture); + } else if (expression.Type == typeof(DateTime) || expression.Type == typeof(DateTimeOffset)) { - return Expression.Call(expression, "ToString", null, Expression.Constant("o")); + return Expression.Call(expression, nameof(ToString), null, Expression.Constant("o")); } else if (expression.Type == typeof(IntPtr) || expression.Type == typeof(TimeSpan)) { - return Expression.Call(expression, "ToString", null); + return Expression.Call(expression, nameof(ToString), null); } else if (IsNullable(expression.Type)) { - var hasValue = Expression.Property(expression, "HasValue"); - var value = Expression.Property(expression, "Value"); + var hasValue = Expression.Property(expression, nameof(Nullable.HasValue)); + var value = Expression.Property(expression, nameof(Nullable.Value)); return Expression.Condition(hasValue, GetDataString(value), Expression.Constant(string.Empty)); } else { - return Expression.Call(expression, "ToString", null, Expression.Constant(CultureInfo.InvariantCulture)); + return Expression.Call(expression, nameof(ToString), null, InvariantCulture); } } @@ -254,7 +263,7 @@ protected override Expression BuildCombinator(IEnumerable arguments) var converterExpression = Expression.Lambda(lineExpression, dataParameter); lineExpression = Expression.Call( typeof(CsvWriter), - "ListJoin", + nameof(ListJoin), new[] { dataType }, inputParameter, converterExpression, @@ -281,7 +290,7 @@ protected override Expression BuildCombinator(IEnumerable arguments) var csvWriter = Expression.Constant(this); var headerExpression = Expression.Constant(header); - return Expression.Call(csvWriter, "Process", new[] { parameterType }, source, headerExpression, writeAction); + return Expression.Call(csvWriter, nameof(Process), new[] { parameterType }, source, headerExpression, writeAction); } static string ListJoin(IList source, Func converter, string separator)