Kelary.Infrastructure/Converters/SiUnitsToStringConverter.cs
/// <summary>
/// Static method for Si units conversion.
/// </summary>
/// <param name="Value"></param>
/// <param name="BaseUnits"></param>
/// <param name="Culture"></param>
/// <param name="Delimiter"></param>
/// <param name="PredefinedPrefix"></param>
/// <returns></returns>
Static method for Si units conversion.
Static method for Si units conversion .
public static string Convert(double Value, string BaseUnits = "", CultureInfo Culture = null, string Delimiter = " ", string Prefix = null)
        {
            if (Culture == null)
                Culture = CultureInfo.CurrentUICulture;
            string[] Prefixes;
            if (Culture.TwoLetterISOLanguageName == "ru")
                Prefixes = new string[] { "ф", "п", "н", "мк", "м", "", "к", "М", "Г", "Т" };
            else
                Prefixes = new string[] { "f", "p", "n", "µ", "m", "", "k", "M", "G", "T" };
            int pow;
            int MinPref = -15 / 3;
            if (Prefix != null)
            {
                pow = Array.IndexOf(Prefixes, Prefix) + MinPref;
            }
            else
            {
                pow = (Value == 0) ? 0 : (int)Math.Floor(Math.Log10(Math.Abs(Value)) / 3);
                pow = Math.Min(Math.Max(pow, MinPref), MinPref + Prefixes.Length - 1);
                Prefix = Prefixes[pow - MinPref];
            }
            if (BaseUnits == "" && Prefix == "")
                Delimiter = "";
            return string.Format(Culture, "{0}{1}{2}{3}", Value / Math.Pow(10, pow * 3), Delimiter, Prefix, BaseUnits);
        }
public static string Convert ( double Value , string BaseUnits = " " , CultureInfo Culture = null , string Delimiter = " " , string Prefix = null ) { if ( Culture == null ) Culture = CultureInfo . CurrentUICulture ; string [ ] Prefixes ; if ( Culture . TwoLetterISOLanguageName == " " ) Prefixes = new string [ ] { " , п " " " м " " , " " " , М " " " Т else Prefixes = new string [ ] { " " , " " , " " , " , m , " , k , M , G , T ; int pow ; int MinPref = - 15 / 3 ; if ( Prefix != null ) { pow = Array . IndexOf ( Prefixes , Prefix ) + MinPref ; } else { pow = ( Value == 0 ) ? 0 : ( int ) Math . Floor ( Math . Log10 ( Math . Abs ( Value ) ) / 3 ) ; pow = Math . Min ( Math . Max ( pow , MinPref ) , MinPref + Prefixes . Length - 1 ) ; Prefix = Prefixes [ pow - MinPref ] ; } if ( BaseUnits == " " && Prefix == " " ) Delimiter = " " ; return string . Format ( Culture , " " , Value / Math . Pow ( 10 , pow * 3 ) , Delimiter , Prefix , BaseUnits ) ; }
Static method for Si units conversion.
Static method for Si units conversion .
// If default prefix specified - use it! // Otherwise determine optimal prefix.
None
Value
double
None
BaseUnits
string
None
Culture
CultureInfo
None
Delimiter
string
None
Prefix
string
PredefinedPrefix
None