Convert long value to KB,MB,GB,TB : long « Data Types « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Data Types » longScreenshots 
Convert long value to KB,MB,GB,TB
 

//Microsoft Reciprocal License (Ms-RL)
//http://bmcommons.codeplex.com/license
using System;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;

namespace BlueMirror.Commons
{

    public static class IntExtensions
    {
        public static string ToRoman(this int number, bool upperCase) {
            return CustomConvert.ToRoman(number, upperCase);
        }
    }

  public static class CustomConvert
  {
    public static string ToRoman(int number, bool upperCase)
    {
      if (number < 0
        throw new ArgumentOutOfRangeException("number", number, "Liczba musi byæ wiêksza od zera.");
      string[] romans = new string[] {"I""IV""V""IX""X""XL""L""XC""C""CD""D""CM""M"};
      // string[] romansLower = new string[] {"i", "iv", "v", "ix", "x", "xl", "l", "xc", "c", "cd", "d", "cm", "m"};

      int[] numbers = new int[] {1459104050901004005009001000};
      int j = 12;
      string result = "";
      // string[] romans = upperCase? romansUpper: romansLower; // za romansUpper i romansLower powstawiac konstruktory tablic - new string[] {}
      while(number != 0)
      {
        if(number >= numbers[j])
        {
          number -= numbers[j];
          result += romans[j];
        }
        else
          j--;
      }
            if (!upperCase)
                result = result.ToLower();
      return result;
    }

    public static string ToSay(double number)
    {
      double floor = Math.Floor(number);
      string result = ToSay(System.Convert.ToInt64(floor)) " i " + ToSay(System.Convert.ToInt64(Math.Round((number - floor100))) " setnych";
      return result;
    }

    public static string ToSay(decimal number, IFormatProvider format)
    {
      string result;
      NumberFormatInfo nfi;
      if(format != null)
        nfi = (NumberFormatInfo)format.GetFormat(typeof(NumberFormatInfo));
      else
        nfi = CultureInfo.CurrentCulture.NumberFormat;
      //if (nfi == null)
      //  throw new Exception("Nie można uzyskaæ obiektu NumberFormatInfo.");
      // TODO: double i decimal - wyprostowaæ.
      double floor = Math.Floor((double)number);
      //long floor = System.Convert.ToInt64(Math.Floor(number));
      result = ToSay(System.Convert.ToInt64(floor)) " " + nfi.CurrencySymbol + " " + ToSay(System.Convert.ToInt64(Math.Round(((double)number - floor100)));
      return result; 
    }

    public static string ToKB(long bytes) {
      string[] suffix = new string[] { "B""KB""MB""GB""TB" };
            float byteNumber = bytes;
      for (int i = 0; i < suffix.Length; i++) {
                if (byteNumber < 1000)
                    if(i == 0)
                        return string.Format("{0} {1}", byteNumber, suffix[i]);
                    else
                        return string.Format("{0:0.#0} {1}", byteNumber, suffix[i]);
        else
                    byteNumber /= 1024;
      }
            return string.Format("{0:N} {1}", byteNumber, suffix[suffix.Length - 1]);
    }

    public static string ToRegex(string wildcard) {
      string result = "^";
      foreach (char chin in wildcard) {
        if (chin == '*')
          result += ".*";
        else if (chin == '?')
          result += ".";
        else if (chin == ';')
          result += "$|^";
        else if ("+()^$.{}[]|\\".IndexOf(chin!= -1)
          result += "\\" + chin;
        else
          result += chin;
      }
      return result + "$";
    
  }
}

   
  
Related examples in the same category
1.Compute the distance from the Earth to the sun, in inches. Compute the distance from the Earth to the sun, in inches.
2.Demonstate automatic conversion from long to doubleDemonstate automatic conversion from long to double
3.Unsigned long value
4.Display value using the standard format specifiers for ulong value
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.