90 lines
2.7 KiB
C#
90 lines
2.7 KiB
C#
|
|
using System;
|
|
using System.Globalization;
|
|
|
|
public class Program
|
|
{
|
|
public static void Main()
|
|
{
|
|
double diameter = 100.5;
|
|
string res = FormatDimNumber(diameter);
|
|
Console.WriteLine($"Diameter 100.5 -> '{res}'");
|
|
|
|
diameter = 100;
|
|
res = FormatDimNumber(diameter);
|
|
Console.WriteLine($"Diameter 100 -> '{res}'");
|
|
|
|
diameter = 0;
|
|
res = FormatDimNumber(diameter);
|
|
Console.WriteLine($"Diameter 0 -> '{res}'");
|
|
|
|
// Test BuildDimensionText
|
|
string dimText = BuildDimensionText($"%%c{FormatDimNumber(100)}", 0.1, -0.1);
|
|
Console.WriteLine($"Text with tol: '{dimText}'");
|
|
|
|
dimText = BuildDimensionText($"%%c{FormatDimNumber(100)}", null, null);
|
|
Console.WriteLine($"Text no tol: '{dimText}'");
|
|
|
|
// Test ScaleParamBag logic (conceptually)
|
|
double scale = 0.5;
|
|
double val = 123.45;
|
|
string scaledStr = (val * scale).ToString("F2");
|
|
Console.WriteLine($"Scaled 123.45 * 0.5 -> '{scaledStr}'");
|
|
|
|
if (double.TryParse(scaledStr, NumberStyles.Float, CultureInfo.InvariantCulture, out var v))
|
|
{
|
|
Console.WriteLine($"Parsed back (Invariant): {v}");
|
|
}
|
|
else if (double.TryParse(scaledStr, NumberStyles.Float, CultureInfo.CurrentCulture, out v))
|
|
{
|
|
Console.WriteLine($"Parsed back (Current): {v}");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Parse failed");
|
|
}
|
|
}
|
|
|
|
private static string FormatDimNumber(double value)
|
|
{
|
|
try
|
|
{
|
|
return value.ToString("0.###");
|
|
}
|
|
catch
|
|
{
|
|
return value.ToString();
|
|
}
|
|
}
|
|
|
|
private static string BuildDimensionText(string baseText, double? tolPlus, double? tolMinus)
|
|
{
|
|
if (!tolPlus.HasValue && !tolMinus.HasValue)
|
|
{
|
|
return baseText;
|
|
}
|
|
|
|
var result = baseText;
|
|
|
|
// 公差格式: 基本尺寸 +上差/-下差
|
|
if (tolPlus.HasValue && tolMinus.HasValue)
|
|
{
|
|
var plusStr = tolPlus.Value >= 0 ? $"+{tolPlus.Value}" : $"{tolPlus.Value}";
|
|
var minusStr = tolMinus.Value >= 0 ? $"+{tolMinus.Value}" : $"{tolMinus.Value}";
|
|
result += $"({plusStr}/{minusStr})";
|
|
}
|
|
else if (tolPlus.HasValue)
|
|
{
|
|
var plusStr = tolPlus.Value >= 0 ? $"+{tolPlus.Value}" : $"{tolPlus.Value}";
|
|
result += $"({plusStr})";
|
|
}
|
|
else if (tolMinus.HasValue)
|
|
{
|
|
var minusStr = tolMinus.Value >= 0 ? $"+{tolMinus.Value}" : $"{tolMinus.Value}";
|
|
result += $"({minusStr})";
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|