Smart rounding was ajusted
This commit is contained in:
16
StructureHelperCommon/Services/DirectRoundLogic.cs
Normal file
16
StructureHelperCommon/Services/DirectRoundLogic.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Services
|
||||
{
|
||||
public class DirectRoundLogic : IMathRoundLogic
|
||||
{
|
||||
public double RoundValue(double value)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
25
StructureHelperCommon/Services/FixedRoundLogic.cs
Normal file
25
StructureHelperCommon/Services/FixedRoundLogic.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Services
|
||||
{
|
||||
public class FixedRoundLogic : IDigitRoundLogic
|
||||
{
|
||||
public int DigitQuant { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Умное окруление до нужного числа значащих цифр, например (12345, 3) дает результат 12300, например (0.12345, 3) дает результат 0,123
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="quant"></param>
|
||||
/// <returns></returns>
|
||||
public double RoundValue(double value)
|
||||
{
|
||||
double roundedValue = Math.Round(value, DigitQuant);
|
||||
return roundedValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
7
StructureHelperCommon/Services/IDigitRoundLogic.cs
Normal file
7
StructureHelperCommon/Services/IDigitRoundLogic.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace StructureHelperCommon.Services
|
||||
{
|
||||
public interface IDigitRoundLogic : IMathRoundLogic
|
||||
{
|
||||
int DigitQuant { get; set; }
|
||||
}
|
||||
}
|
||||
7
StructureHelperCommon/Services/IMathRoundLogic.cs
Normal file
7
StructureHelperCommon/Services/IMathRoundLogic.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace StructureHelperCommon.Services
|
||||
{
|
||||
public interface IMathRoundLogic
|
||||
{
|
||||
double RoundValue(double value);
|
||||
}
|
||||
}
|
||||
30
StructureHelperCommon/Services/SmartRoundLogic.cs
Normal file
30
StructureHelperCommon/Services/SmartRoundLogic.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Services
|
||||
{
|
||||
public class SmartRoundLogic : IDigitRoundLogic
|
||||
{
|
||||
public int DigitQuant { get; set; } = 3;
|
||||
|
||||
/// <summary>
|
||||
/// Умное окруление до нужного числа значащих цифр, например (12345, 3) дает результат 12300, например (0.12345, 3) дает результат 0,123
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="quant"></param>
|
||||
/// <returns></returns>
|
||||
public double RoundValue(double value)
|
||||
{
|
||||
if (value == 0d) return 0d;
|
||||
double valueOrder = Math.Log10(Math.Abs(value));
|
||||
int order = Convert.ToInt32(Math.Ceiling(valueOrder));
|
||||
double requiredOrder = Math.Pow(10, DigitQuant - order);
|
||||
double roundedAbsValue = Math.Round(Math.Abs(value) * requiredOrder) / requiredOrder;
|
||||
double roundedValue = Math.Sign(value) * roundedAbsValue;
|
||||
return roundedValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,116 +0,0 @@
|
||||
using StructureHelperCommon.Infrastructures.Enums;
|
||||
using StructureHelperCommon.Infrastructures.Exceptions;
|
||||
using StructureHelperCommon.Services.Units;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Windows.Documents;
|
||||
|
||||
namespace StructureHelperCommon.Services.Units
|
||||
{
|
||||
public static class CommonOperation
|
||||
{
|
||||
private static IEnumerable<IUnit> units = UnitsFactory.GetUnitCollection();
|
||||
|
||||
public static double ConvertToDoubleChangeComma(string s)
|
||||
{
|
||||
double result;
|
||||
if (!double.TryParse(s, NumberStyles.Any, CultureInfo.CurrentCulture, out result) &&
|
||||
!double.TryParse(s, NumberStyles.Any, CultureInfo.GetCultureInfo("en-US"), out result) &&
|
||||
!double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out result))
|
||||
{
|
||||
throw new StructureHelperException($"{ErrorStrings.IncorrectValue}: {s}");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static IStringDoublePair DivideIntoStringDoublePair(string s)
|
||||
{
|
||||
s = s.Replace(" ", "");
|
||||
//string digitPattern = @"^[-]?[+]?\d+(\.?)|(\,?)\d*";
|
||||
string digitPattern = @"^[-]?[+]?\d*\.?\,?\d*";
|
||||
string textPattern = @"[0-9]|\.|\,";
|
||||
string caracterPattern = "[a-z]+$";
|
||||
string target = "";
|
||||
Regex regexText = new Regex(textPattern);
|
||||
string textString = regexText.Replace(s, target);
|
||||
var textMatch = Regex.Match(textString, caracterPattern, RegexOptions.IgnoreCase);
|
||||
if (textMatch.Success) {textString = textMatch.Value.ToLower();}
|
||||
var match = Regex.Match(s, digitPattern);
|
||||
if (match.Success)
|
||||
{
|
||||
string digitString = match.Value;
|
||||
double digit = ConvertToDoubleChangeComma(digitString);
|
||||
return new StringDoublePair() { Digit = digit, Text = textString };
|
||||
}
|
||||
throw new StructureHelperException(ErrorStrings.DataIsInCorrect);
|
||||
}
|
||||
|
||||
public static IUnit GetUnit(UnitTypes unitType, string unitName = null)
|
||||
{
|
||||
if (unitName is null)
|
||||
{
|
||||
var boolResult = DefaultUnitNames.TryGetValue(unitType, out unitName);
|
||||
if (boolResult == false)
|
||||
{
|
||||
throw new StructureHelperException(ErrorStrings.DataIsInCorrect + $": unit type{unitType} is unknown");
|
||||
}
|
||||
}
|
||||
return units.Where(u => u.UnitType == unitType & u.Name == unitName).Single();
|
||||
}
|
||||
|
||||
public static Dictionary<UnitTypes, string> DefaultUnitNames => new()
|
||||
{
|
||||
{ UnitTypes.Length, "m"},
|
||||
{ UnitTypes.Area, "m2"},
|
||||
{ UnitTypes.Force, "kN" },
|
||||
{ UnitTypes.Moment, "kNm"},
|
||||
{ UnitTypes.Stress, "MPa"},
|
||||
{ UnitTypes.Curvature, "1/m"},
|
||||
};
|
||||
|
||||
|
||||
public static string Convert(IUnit unit, string unitName, object value)
|
||||
{
|
||||
double val;
|
||||
if (value != null) { val = (double)value; }
|
||||
else { throw new Exception($"{unitName} value is null"); }
|
||||
val *= unit.Multiplyer;
|
||||
string strValue = $"{val} {unit.Name}";
|
||||
return strValue;
|
||||
}
|
||||
|
||||
public static double ConvertBack(UnitTypes unitType, IUnit unit, object value)
|
||||
{
|
||||
double val;
|
||||
double multy;
|
||||
double coefficient = unit.Multiplyer;
|
||||
var strVal = value as string;
|
||||
IStringDoublePair pair = DivideIntoStringDoublePair(strVal);
|
||||
try
|
||||
{
|
||||
multy = GetMultiplyer(unitType, pair.Text);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
multy = coefficient;
|
||||
}
|
||||
val = pair.Digit / multy;
|
||||
return val;
|
||||
}
|
||||
|
||||
public static double GetMultiplyer(UnitTypes unitType, string unitName)
|
||||
{
|
||||
try
|
||||
{
|
||||
return units.Where(u => u.UnitType == unitType & u.Name == unitName).Single().Multiplyer;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new StructureHelperException(ErrorStrings.DataIsInCorrect + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
85
StructureHelperCommon/Services/Units/ConvertUnitLogic.cs
Normal file
85
StructureHelperCommon/Services/Units/ConvertUnitLogic.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using StructureHelperCommon.Infrastructures.Enums;
|
||||
using StructureHelperCommon.Infrastructures.Exceptions;
|
||||
using StructureHelperCommon.Models.Parameters;
|
||||
using StructureHelperCommon.Services.Units;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Windows.Documents;
|
||||
|
||||
namespace StructureHelperCommon.Services.Units
|
||||
{
|
||||
public class ConvertUnitLogic : IConvertUnitLogic
|
||||
{
|
||||
private static IEnumerable<IUnit> units = UnitsFactory.GetUnitCollection();
|
||||
private static IProcessValuePairLogic<double> pairLogic = new ProcessDoublePairLogic();
|
||||
|
||||
public IMathRoundLogic MathRoundLogic { get; set; } = new DirectRoundLogic();
|
||||
|
||||
public ValuePair<double> Convert(IUnit unit, string unitName, object value)
|
||||
{
|
||||
double val;
|
||||
if (value != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
val = (double)value;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new StructureHelperException($"{ErrorStrings.IncorrectValue}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new StructureHelperException($"{ErrorStrings.ParameterIsNull}: {unitName}");
|
||||
}
|
||||
val *= unit.Multiplyer;
|
||||
var pair = new ValuePair<double>
|
||||
{
|
||||
Text = unit.Name,
|
||||
Value = val
|
||||
};
|
||||
return pair;
|
||||
}
|
||||
|
||||
public double ConvertBack(UnitTypes unitType, IUnit unit, object value)
|
||||
{
|
||||
double val;
|
||||
double multy;
|
||||
double factor = unit.Multiplyer;
|
||||
var strVal = value as string;
|
||||
var pair = pairLogic.GetValuePairByString(strVal);
|
||||
try
|
||||
{
|
||||
multy = GetMultiplyer(unitType, pair.Text);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
multy = factor;
|
||||
}
|
||||
val = pair.Value / multy;
|
||||
return val;
|
||||
}
|
||||
|
||||
private double GetMultiplyer(UnitTypes unitType, string unitName)
|
||||
{
|
||||
try
|
||||
{
|
||||
return units
|
||||
.Where(u =>
|
||||
u.UnitType == unitType
|
||||
&
|
||||
u.Name == unitName)
|
||||
.Single()
|
||||
.Multiplyer;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new StructureHelperException(ErrorStrings.DataIsInCorrect + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
50
StructureHelperCommon/Services/Units/GetUnitLogic.cs
Normal file
50
StructureHelperCommon/Services/Units/GetUnitLogic.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using StructureHelperCommon.Infrastructures.Enums;
|
||||
using StructureHelperCommon.Infrastructures.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Services.Units
|
||||
{
|
||||
public class GetUnitLogic : IGetUnitLogic
|
||||
{
|
||||
private static IEnumerable<IUnit> units = UnitsFactory.GetUnitCollection();
|
||||
private Dictionary<UnitTypes, string> defaultUnitNames;
|
||||
|
||||
|
||||
public GetUnitLogic()
|
||||
{
|
||||
defaultUnitNames = new()
|
||||
{
|
||||
{ UnitTypes.Length, "m"},
|
||||
{ UnitTypes.Area, "m2"},
|
||||
{ UnitTypes.Force, "kN" },
|
||||
{ UnitTypes.Moment, "kNm"},
|
||||
{ UnitTypes.Stress, "MPa"},
|
||||
{ UnitTypes.Curvature, "1/m"},
|
||||
};
|
||||
}
|
||||
|
||||
public IUnit GetUnit(UnitTypes unitType, string unitName = null)
|
||||
{
|
||||
if (unitName is null)
|
||||
{
|
||||
var boolResult = defaultUnitNames.TryGetValue(unitType, out unitName);
|
||||
if (boolResult == false)
|
||||
{
|
||||
throw new StructureHelperException(ErrorStrings.DataIsInCorrect + $": unit type{unitType} is unknown");
|
||||
}
|
||||
}
|
||||
return units
|
||||
.Where(u =>
|
||||
u.UnitType == unitType
|
||||
&
|
||||
u.Name == unitName)
|
||||
.Single();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
13
StructureHelperCommon/Services/Units/IConvertUnitLogic.cs
Normal file
13
StructureHelperCommon/Services/Units/IConvertUnitLogic.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using StructureHelperCommon.Infrastructures.Enums;
|
||||
using StructureHelperCommon.Models.Parameters;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace StructureHelperCommon.Services.Units
|
||||
{
|
||||
public interface IConvertUnitLogic
|
||||
{
|
||||
IMathRoundLogic MathRoundLogic { get; set; }
|
||||
ValuePair<double> Convert(IUnit unit, string unitName, object value);
|
||||
double ConvertBack(UnitTypes unitType, IUnit unit, object value);
|
||||
}
|
||||
}
|
||||
9
StructureHelperCommon/Services/Units/IGetUnitLogic.cs
Normal file
9
StructureHelperCommon/Services/Units/IGetUnitLogic.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using StructureHelperCommon.Infrastructures.Enums;
|
||||
|
||||
namespace StructureHelperCommon.Services.Units
|
||||
{
|
||||
public interface IGetUnitLogic
|
||||
{
|
||||
IUnit GetUnit(UnitTypes unitType, string unitName = null);
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Services.Units
|
||||
{
|
||||
public interface IStringDoublePair
|
||||
{
|
||||
double Digit { get; }
|
||||
string Text { get; }
|
||||
}
|
||||
}
|
||||
25
StructureHelperCommon/Services/Units/ProcessString.cs
Normal file
25
StructureHelperCommon/Services/Units/ProcessString.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using StructureHelperCommon.Infrastructures.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Services.Units
|
||||
{
|
||||
public static class ProcessString
|
||||
{
|
||||
public static double ConvertCommaToCultureSettings(string s)
|
||||
{
|
||||
double result;
|
||||
if (!double.TryParse(s, NumberStyles.Any, CultureInfo.CurrentCulture, out result) &&
|
||||
!double.TryParse(s, NumberStyles.Any, CultureInfo.GetCultureInfo("en-US"), out result) &&
|
||||
!double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out result))
|
||||
{
|
||||
throw new StructureHelperException($"{ErrorStrings.IncorrectValue}: {s}");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Services.Units
|
||||
{
|
||||
internal class StringDoublePair : IStringDoublePair
|
||||
{
|
||||
public double Digit { get; set; }
|
||||
public string Text { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,10 @@ namespace StructureHelperCommon.Services.Units
|
||||
{
|
||||
public static class UnitsFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns collection of unit
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static List<IUnit> GetUnitCollection()
|
||||
{
|
||||
List<IUnit> units = new List<IUnit>();
|
||||
@@ -28,8 +32,8 @@ namespace StructureHelperCommon.Services.Units
|
||||
type = UnitTypes.Moment;
|
||||
units.Add(new Unit() { UnitType = type, Name = "Nm", Multiplyer = 1d });
|
||||
units.Add(new Unit() { UnitType = type, Name = "kNm", Multiplyer = 1e-3d });
|
||||
units.Add(new Unit() { UnitType = type, Name = "kgfm", Multiplyer = 9.8d });
|
||||
units.Add(new Unit() { UnitType = type, Name = "tfm", Multiplyer = 9.8e-3d });
|
||||
units.Add(new Unit() { UnitType = type, Name = "kgfm", Multiplyer = 9.81d });
|
||||
units.Add(new Unit() { UnitType = type, Name = "tfm", Multiplyer = 9.81e-3d });
|
||||
type = UnitTypes.Curvature;
|
||||
units.Add(new Unit() { UnitType = type, Name = "1/m", Multiplyer = 1d });
|
||||
units.Add(new Unit() { UnitType = type, Name = "1/mm", Multiplyer = 1e-3d });
|
||||
|
||||
Reference in New Issue
Block a user