private static BigInteger BytesToAppropriateByteUnit(BigInteger value, UnitsOfInformationRadix radix, out UnitsOfInformation uoi) { switch (radix) { case UnitsOfInformationRadix.Decimal: if (value < 1000) { uoi = UnitsOfInformation.bytes; return(value); } value /= 1000; if (value < 1000) { uoi = UnitsOfInformation.kilobyte; return(value); } value /= 1000; if (value < 1000) { uoi = UnitsOfInformation.megabyte; return(value); } value /= 1000; if (value < 1000) { uoi = UnitsOfInformation.gigabyte; return(value); } value /= 1000; if (value < 1000) { uoi = UnitsOfInformation.terabyte; return(value); } value /= 1000; if (value < 1000) { uoi = UnitsOfInformation.petabyte; return(value); } value /= 1000; if (value < 1000) { uoi = UnitsOfInformation.exabyte; return(value); } value /= 1000; if (value < 1000) { uoi = UnitsOfInformation.zettabyte; return(value); } value /= 1000; if (value < 1000) { uoi = UnitsOfInformation.yottabyte; return(value); } value /= 1000; uoi = UnitsOfInformation.bytes; return(value); case UnitsOfInformationRadix.Binary: if (value < 1024) { uoi = UnitsOfInformation.bytes; return(value); } value /= 1024; if (value < 1024) { uoi = UnitsOfInformation.kibibyte; return(value); } value /= 1024; if (value < 1024) { uoi = UnitsOfInformation.mebibyte; return(value); } value /= 1024; if (value < 1024) { uoi = UnitsOfInformation.gibibyte; return(value); } value /= 1024; if (value < 1024) { uoi = UnitsOfInformation.tebibyte; return(value); } value /= 1024; if (value < 1024) { uoi = UnitsOfInformation.pebibyte; return(value); } value /= 1024; if (value < 1024) { uoi = UnitsOfInformation.exbibyte; return(value); } value /= 1024; if (value < 1024) { uoi = UnitsOfInformation.zebibyte; return(value); } value /= 1024; if (value < 1024) { uoi = UnitsOfInformation.yobibyte; return(value); } value /= 1024; uoi = UnitsOfInformation.bytes; return(value); } uoi = UnitsOfInformation.bytes; return(value); }
/// <summary>Converts the number to a unit of information.<para>Ansprechpartner: Henry de Jongh</para></summary> /// <param name="value">The value to convert.</param> /// <param name="from">The unit of information to convert from.</param> /// <param name="radix">The desired radix to use.</param> /// <returns>The unit of information as string.</returns> public static string ToUnitOfInformation(this ushort value, UnitsOfInformation from, UnitsOfInformationRadix radix) => ConvertToUnitOfInformation(value, from, radix);
/// <summary>Converts the number to an appropriate unit of information.</summary> /// <param name="value">The value to convert.</param> /// <param name="from">The unit of information to convert from.</param> /// <param name="radix">The desired radix to use.</param> /// <returns>The unit of information as string.</returns> private static string ConvertToUnitOfInformation(BigInteger value, UnitsOfInformation from, UnitsOfInformationRadix radix) { bool fromIsBits = IsBits(from); bool fromIsBytes = IsBytes(from); // result in bits if (fromIsBits) { // convert value "from" of any bit-unit to bits. value = AnyBitUnitToBits(value, from); // convert the bits to the "to" unit. return(BitsToAppropriateBitUnit(value, radix, out UnitsOfInformation uoi) + " " + GetPrefix(uoi)); } // result in bytes if (fromIsBytes) { // convert value "from" of any byte-unit to byte. value = AnyByteUnitToBytes(value, from); // convert the bytes to the "to" unit. return(BytesToAppropriateByteUnit(value, radix, out UnitsOfInformation uoi) + " " + GetPrefix(uoi)); } // should never happen: return(value + " ???"); }
/// <summary>Converts the number to a unit of information.<para>Ansprechpartner: Henry de Jongh</para></summary> /// <param name="value">The value to convert.</param> /// <param name="from">The unit of information to convert from.</param> /// <param name="radix">The desired radix to use.</param> /// <returns>The unit of information as string.</returns> public static string ToUnitOfInformation(this float value, UnitsOfInformation from, UnitsOfInformationRadix radix) => ConvertToUnitOfInformation((BigInteger)value, from, radix);