LunarMind/Converts/ScaleConverter.cs

(完整代码) 1KB

using System;
using System.Globalization;
using System.Windows.Data;
using System.Text;

namespace SHomeWorkshop.LunarMind.Converts
{
    /// <summary>
    /// 以百分比显示,因此要乘以100.
    /// </summary>
    [ValueConversion(typeof(double), typeof(string))]
    public class ScaleConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return "100%";

            double old = (double)value;
            double newValue;

            if (old == 0) newValue = 1;
            else if (old < 0)
            {
                newValue = 1 + old * (0.1);
            }
            else
            {
                newValue = old / 2 + 1;
            }

            StringBuilder sb = new StringBuilder();
            sb.Append(((double)newValue * 100).ToString("f1"));
            sb.Append("%");

            return sb.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }

        #endregion
    }
}

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。