50 lines
1.5 KiB
C#
50 lines
1.5 KiB
C#
using System;
|
||
using System.Globalization;
|
||
using System.Windows.Data;
|
||
|
||
namespace XP.Common.Controls.ImageHistogram
|
||
{
|
||
/// <summary>
|
||
/// 频次标签转换器:将大数值转为 K/M 缩写格式 | Frequency label converter: converts large values to K/M abbreviation format
|
||
/// 例如:500000 → "500K",1500000 → "1.5M",800 → "800"
|
||
/// </summary>
|
||
internal sealed class FrequencyLabelConverter : IValueConverter
|
||
{
|
||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||
{
|
||
if (value == null) return "0";
|
||
|
||
double num;
|
||
if (value is double d)
|
||
num = d;
|
||
else if (value is decimal dec)
|
||
num = (double)dec;
|
||
else if (!double.TryParse(value.ToString(), out num))
|
||
return value.ToString() ?? "0";
|
||
|
||
if (num >= 1_000_000)
|
||
{
|
||
double mValue = num / 1_000_000.0;
|
||
return mValue == Math.Floor(mValue)
|
||
? $"{(int)mValue}M"
|
||
: $"{mValue:0.#}M";
|
||
}
|
||
|
||
if (num >= 1_000)
|
||
{
|
||
double kValue = num / 1_000.0;
|
||
return kValue == Math.Floor(kValue)
|
||
? $"{(int)kValue}K"
|
||
: $"{kValue:0.#}K";
|
||
}
|
||
|
||
return $"{(int)num}";
|
||
}
|
||
|
||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||
{
|
||
throw new NotImplementedException();
|
||
}
|
||
}
|
||
}
|