代码之家  ›  专栏  ›  技术社区  ›  Grahame A

字符串格式没有美分的货币格式

  •  44
  • Grahame A  · 技术社区  · 14 年前

    我正在使用当前方法显示货币

    String.Format("{0:C}", item.DonationAmount)
    

    大概10美元

    我们将处理大量的美元,而不是美分。我们希望货币显示为10美元,不带小数点或零。我该怎么做?删除货币格式会使数字显示为10.0000:(提前谢谢。

    3 回复  |  直到 3 年前
        1
  •  119
  •   stuartd saeed    14 年前

    指定需要零位小数:

    String.Format("{0:C0}", item.DonationAmount)
    
        2
  •  15
  •   Serj Sagan    5 年前

    item.DonationAmount 是一个 non-nullable decimal 然后你可以做:

    item.DonationAmount.ToString("c0");
    

    物品捐赠金额 是一个 nullable 您选中的有一个值,然后您可以执行以下操作:

    item.DonationAmount.Value.ToString("c0");
    

    或者更新版本的 C#

    item.DonationAmount?.ToString("c0");
    
        3
  •  6
  •   Bhanu Pratap    8 年前
      decimal value = 0.00M;
            value = Convert.ToDecimal(12345.12345);
            Console.WriteLine(".ToString(\"C\") Formates With Currency $ Sign");
            Console.WriteLine(value.ToString("C"));
            //OutPut : $12345.12
            Console.WriteLine(value.ToString("C1"));
            //OutPut : $12345.1
            Console.WriteLine(value.ToString("C2"));
            //OutPut : $12345.12
            Console.WriteLine(value.ToString("C3"));
            //OutPut : $12345.123
            Console.WriteLine(value.ToString("C4"));
            //OutPut : $12345.1235
            Console.WriteLine(value.ToString("C5"));
            //OutPut : $12345.12345
            Console.WriteLine(value.ToString("C6"));
            //OutPut : $12345.123450
            Console.WriteLine();
            Console.WriteLine(".ToString(\"F\") Formates With out Currency Sign");
            Console.WriteLine(value.ToString("F"));
            //OutPut : 12345.12
            Console.WriteLine(value.ToString("F1"));
            //OutPut : 12345.1
            Console.WriteLine(value.ToString("F2"));
            //OutPut : 12345.12
            Console.WriteLine(value.ToString("F3"));
            //OutPut : 12345.123
            Console.WriteLine(value.ToString("F4"));
            //OutPut : 12345.1235
            Console.WriteLine(value.ToString("F5"));
            //OutPut : 12345.12345
            Console.WriteLine(value.ToString("F6"));
            //OutPut : 12345.123450
    
            Console.WriteLine();
            Console.WriteLine(".ToString(\"N\") Formates With out Currency Sign");
            Console.WriteLine(value.ToString("N"));
            //OutPut : 12,345.12
            Console.WriteLine(value.ToString("N1"));
            //OutPut : 12,345.1
            Console.WriteLine(value.ToString("N2"));
            //OutPut : 12,345.12
            Console.WriteLine(value.ToString("N3"));
            //OutPut : 12,345.123
            Console.WriteLine(value.ToString("N4"));
            //OutPut : 12,345.1235
            Console.WriteLine(value.ToString("N5"));
            //OutPut : 12,345.12345
            Console.WriteLine(value.ToString("N6"));
            //OutPut : 12,345.123450
    
            Console.WriteLine();
            Console.WriteLine(".ToString(\"P\") Formates With Percentage (i.e multiply by 100)");
            Console.WriteLine(value.ToString("P"));
            //OutPut : 1,2345,12.35%
            Console.WriteLine(value.ToString("P1"));
            //OutPut : 1,2345,12.3%
            Console.WriteLine(value.ToString("P2"));
            //OutPut : 12345,12.35%
            Console.WriteLine(value.ToString("P3"));
            //OutPut : 12345,12.345%
            Console.WriteLine(value.ToString("P4"));
            //OutPut : 12345,12.3450%
            Console.WriteLine(value.ToString("P5"));
            //OutPut : 12345,12.34500%
            Console.WriteLine(value.ToString("P6"));
            //OutPut : 12345,12.345000%
    
    
            Console.Read();
    

    Click to see Console Out Put Screen