79 lines
3.0 KiB
C#
79 lines
3.0 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Windows.Data;
|
|
|
|
namespace CordonelAssemblyLine.Converter
|
|
{
|
|
/// <inheritdoc />
|
|
/// <summary>
|
|
/// Conversion between strings and doubles
|
|
/// </summary>
|
|
public class MathConverter : IValueConverter
|
|
{
|
|
/// <summary>
|
|
/// Converts a string to double which contains a pure floating point value
|
|
/// </summary>
|
|
/// <param name="value">string containing the double</param>
|
|
/// <param name="targetType">UNUSED</param>
|
|
/// <param name="parameter">UNUSED</param>
|
|
/// <param name="culture"></param>
|
|
/// <returns>value in double format</returns>
|
|
public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
|
|
{
|
|
return double.Parse((String)value ?? string.Empty, CultureInfo.CurrentUICulture);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a string containing an exponential value to a pure double returned as string
|
|
/// </summary>
|
|
/// <param name="value"></param>
|
|
/// <param name="targetType"></param>
|
|
/// <param name="parameter"></param>
|
|
/// <param name="culture"></param>
|
|
/// <returns></returns>
|
|
public Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture)
|
|
{
|
|
String format = "";
|
|
String strValue = (String)value;
|
|
if (strValue != null)
|
|
{
|
|
strValue = strValue.Replace("x", "*").Replace("X", "*");
|
|
|
|
//2E-07
|
|
if (strValue.ToUpper().Contains("E"))
|
|
{
|
|
return double.Parse(strValue, CultureInfo.InvariantCulture).ToString(format);
|
|
}
|
|
|
|
//2*10^-3
|
|
if (strValue.Contains("^-") && strValue.Contains("*"))
|
|
{
|
|
var split = strValue.Split('*');
|
|
var multiply = double.Parse(split.First());
|
|
var powNumber = double.Parse(split.Last().Split('^').First());
|
|
var powBy = double.Parse(split.Last().Split('^').Last());
|
|
return (multiply * Math.Pow(powNumber, powBy)).ToString(format);
|
|
}
|
|
|
|
|
|
if (strValue.Contains("-") && strValue.Contains("*"))
|
|
{
|
|
var split = strValue.Split('*');
|
|
var multiply = double.Parse(split.First());
|
|
var powNumber = double.Parse(split.Last().Split('-').First());
|
|
var powBy = double.Parse(split.Last().Split('-').Last());
|
|
powBy *= -1;
|
|
return (multiply * Math.Pow(powNumber, powBy)).ToString(format);
|
|
}
|
|
|
|
if (strValue.Contains("0,0") || strValue.Contains("0.0"))
|
|
{
|
|
return double.Parse(strValue, CultureInfo.CurrentUICulture).ToString(format);
|
|
}
|
|
}
|
|
|
|
return value;
|
|
}
|
|
}
|
|
} |