2011-05-01 40 views
3

我有一個存儲在類型爲「long」的變量中的值。c中long到decimal的轉換#

long fileSizeInBytes = FileUploadControl.FileContent.Length; 
Decimal fileSizeInMB = Convert.ToDecimal(fileSizeInBytes/(1024 * 1024)); 

我想給fileSizeInBytes轉換爲四捨五入至小數點後2位十進制數(這樣的:2.45 3.51 1.74,,),但我沒能獲得所需的結果。作爲結果,我只能得到沒有小數位的單個數字。有人能幫助我嗎?

感謝預期

+0

長只是一個大整數,它沒有超過小數點。給出一個例子,說明你正在得到什麼,你期待什麼,我認爲你很長時間會與另一種類型混淆。 – Milimetric 2011-05-01 19:02:46

+0

@ robilit建議@Milimetric我試圖做他所建議的。例如,如果我劃分20364702 /(1024.0米* 1024.0米)這是我得到的結果:19.4212932586669921875M但我想要的結果是19.42。我希望結果四捨五入到小數點後兩位。我怎樣才能做到這一點 ?? – 2011-05-01 19:12:11

回答

13
Decimal fileSizeInMB = Convert.ToDecimal(fileSize)/(1024.0m * 1024.0m); 

你在做什麼是一個整數,這導致了整數,不是一個小數分頻的文件大小。剩下的剩餘部分將被切斷。

+1

@但我想結果四捨五入到小數位。如果我劃分20364702 /(1024.0米* 1024.0米),這是我得到的結果:19.4212932586669921875M但我想要的結果是19.42。我怎麼能得到這個? – 2011-05-01 19:06:22

+3

@ user653622你可以使用Math.Round(fileSizeInMB,2)是一種方法 – anishMarokey 2011-05-01 19:12:26

+0

@anishMarokey非常感謝你! – 2011-05-01 19:16:14

1

我沒有看到fileSize在任何地方聲明 - 但我會認爲這是一個漫長的。所以fileSize/(1024 * 1024)被迫高達所以你喜歡的東西不抱任何小數的長期價值:

Convert.ToDecimal(someLongValue) 

,不會有任何小數。首先它傳遞給Convert.ToDecimal

1

這裏之前轉換劃分爲雙(或其他十進制)是一個功能我用它來顯示文件大小

//--------------------------------------------------------------------------------------------------------------------------------- 
     /// <summary> 
     /// Formats from bytes to KB,MB,GB,TB 
     /// </summary> 
     /// <param name="number">Bytes to format</param> 
     /// <returns></returns> 
     public static string AutoFileSize(long number) 
     { 
      double tmp = number; 
      string suffix = " B "; 
      if (tmp > 1024) { tmp = tmp/1024; suffix = " KB"; } 
      if (tmp > 1024) { tmp = tmp/1024; suffix = " MB"; } 
      if (tmp > 1024) { tmp = tmp/1024; suffix = " GB"; } 
      if (tmp > 1024) { tmp = tmp/1024; suffix = " TB"; } 
      return tmp.ToString("n") + suffix; 
     } 
2

也許有點晚,但在這裏你有更多或更少一切你需要的filesizes瓦等

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Globalization; 
using System.Runtime.InteropServices; 
using System.Threading; 

namespace ConsoleApplication1 
{ 

    class Program 
    { 

     /// <summary> 
     /// Formats the given size to the order of magniture given 
     /// Order is 1 for KB, 2 for MB etc up to 8, after that you get exponents for the same notations 
     /// </summary> 
     /// <param name="size">The total size in bytes</param> 
     /// <param name="order">+1 for each 1024 B,M,... 0 for nothing</param> 
     /// <param name="unit">Usually you will want B for bytes denotation, but maybe "bit" or "bi" or W for watt</param> 
     /// <param name="decimal_places">Number of desired decimal places</param> 
     /// <param name="add_space">Separate KB MB etc from the number with a space?</param> 
     /// <returns>Formatted size</returns> 
     public static string FormatSize(string unit, double size, int order, int decimal_places, bool add_space) { 

      string[] suffixes = new string[] {"", "K","M","G","T","P","E","Z","Y"}; 

      int exponent = order - 8 > 0 ? order - 8 : 0; 
      order -= exponent; 

      string suffix = suffixes[order]; 

      while (order > 0) { 
       size /= 1024; 
       order--; 
      } 

      string sDecimals = new String('0', decimal_places); 
      string sExponent = exponent != 0 ? "E" + exponent : ""; 
      string dot = decimal_places > 0 ? "." : ""; 

      return size.ToString("#,##0" + dot + sDecimals + sExponent) + (add_space ? " " : "") + suffix + unit; 
     } 

     public static void Main(string[] Args) 
     { 
      Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; 

      string sz; 
      sz = FormatSize("B", 1024, 1, 0, false); 
      Console.WriteLine(sz); 
      sz = FormatSize("B", 1024*1024 + 512, 1, 0, true); 
      Console.WriteLine(sz); 
      sz = FormatSize("W", 1024 * 1024 + 512, 1, 2, true); 
      Console.WriteLine(sz); 
      sz = FormatSize("B", 1024 * 1024 + 512, 2, 2, true); 
      Console.WriteLine(sz); 
      sz = FormatSize("B", 1024 * 1024 * 1024 + 1024 * 1024 * 1024/2, 3, 0, false); 
      Console.WriteLine(sz); 
      sz = FormatSize("bit", 1024 * 1024 * 1024 + 1024 * 1024 * 1024/2, 3, 1, false); 
      Console.WriteLine(sz); 
      sz = FormatSize("B", 1024 * 1024 * 1024 + 1024 * 1024 * 1024/2 - 1, 3, 2, false); 
      Console.WriteLine(sz); 
      sz = FormatSize("Ω", 1024 * 1024 * 1024 + 1024 * 1024 * 1024/2 - 1, 3, 1, false); 
      Console.WriteLine(sz); 
      sz = FormatSize("B", 1024 * 1024 * 1024 + 1024 * 1024 * 1024/2 - 10000000, 3, 2, false); 
      Console.WriteLine(sz); 
      sz = FormatSize("B", 1024 * 1024 * 1024 + 1024 * 1024 * 1024/2 - 1, 3, 0, false); 
      Console.WriteLine(sz); 
      sz = FormatSize("bit", 1208925819614629174706176f, 9, 2, true); 
      Console.WriteLine(sz); 
     } 
    } 
} 

下面是一些輸出:

1KB 
1,025 KB 
1,024.50 KW 
1.00 MB 
2GB 
1.5Gbit 
1.50GB 
1.5GΩ 
1.49GB 
1GB 
1.00E1 Ybit 
相關問題